\input botex

@c Copyright (C) 1985 Richard M. Stallman.

@ignore
Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.

Permission is granted to process this file through Tex and print the
results, provided the printed document carries copying permission
notice identical to this one except for the removal of this paragraph
(this paragraph not being relevant to the printed manual).

Permission is granted to copy and distribute modified versions of
this manual under the conditions in the preceding paragraph,
provided that the section entitled ``The GNU Manifesto''
is included exactly as in the original, and provided that
the entire resulting derived work is distributed under the terms of
a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, provided that the section entitled ``The GNU
Manifesto'' is included, either in English exactly
as in the original, or in a translation approved by the author.
@end ignore

@c This file will eventually be the source of the Info documentation
@c for Emacs; but it needs some more work to do that properly,
@c and some incompatibilities between botex and texinfo must be fixed. 

@c @kbd should prevent single-letter words inside it from affecting
@c following periods.

@defindex cp
@defindex cf
@defindex vr
@defindex kw
@c kw index is used for keys

@hsize = 6.5in
@parindent 15pt
@parskip 18pt
@aboveenvskipamount 0pt
@baselineskip 15pt
@itemindent = 0.3in
@tableindent = 0.8in
@lispnarrowing = 0.4in
@setchapternewpage odd
@settitle{GNU Emacs Manual}
@tex
\gdef\iftex{}
\gdef\Eiftex{}
\long\gdef\ifinfo #1\end ifinfo{}
\long\gdef\menu #1\end menu{}
\gdef\var{\parsearg\iF}
\gdef\dfn{\parsearg\iF}
\gdef\ctl{\parsearg\ctlX}
\gdef\ctlX #1{{\tt \hat}#1}
\gdef\code{\parsearg\codeX}
\gdef\codeX #1{{\li #1}}
\gdef\kbd{\parsearg\kbdF}
\gdef\ttfont{\parsearg\kbdF}
\gdef\kbdF #1{{\tt #1}}
\gdef\samp{\parsearg\sampF}
\gdef\sampF #1{`{\tt #1}'}
\gdef\key{\parsearg\termkeyX}
\gdef\termkeyX #1{{\tt \uppercase{#1}}}
\gdef\node [#1]{}
\gdef\refill {}
\gdef\dots{$\ldots$}
\gdef\pxref [#1]{see \xrefX [#1,,,,,]}
\gdef\xref [#1]{See \xrefX [#1,,,,,]}
\gdef\xrefX [#1,#2,#3,#4]{\refx{#1-snt} [%
{\setbox0=\hbox{#3}%
\ifdim \wd0 =0pt #1\else\unhbox0\fi}%
], page\tie \refx{#1-pg}}
\gdef\setref{\parsearg\setrefX}
{\catcode `\-=11
\gdef\setrefX#1{%
\dosetq{#1-pg}{page-number}%
\dosetq{#1-snt}{section-number-and-type}}}
@end tex

@c Define(Commands=Description,LeftMargin 16,Indent -8,Spread 0)
@c Define(WideCommands=Description,LeftMargin 20,Indent -12,Spread 0)
@c Define(DoubleWideCommands=Description,LeftMargin 24,Indent -16,Spread 0)
@c Case[device,
@c Dover=<@Define(GrossCommands=Description,LeftMargin 16,Indent -16,Spread 3pts)>,
@c Else=<@Define(GrossCommands=Description,LeftMargin 16,Indent -16,Spread 1)>
@c ]

@iftex
@tex
\hbox{}
@end tex
@kern 1.5in
@center 9GNU Emacs Manual*
@sp 4
@center First Edition, Emacs Version 16
@sp 1
@center June 1985
@sp 5
@center Richard Stallman
@page
@tex
\hbox{}
@end tex
@kern 4.5in
@nopara
Copyright (C) 1985 Richard M. Stallman.

Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.

Permission is granted to copy and distribute modified versions of
this manual under the conditions in the preceding paragraph,
provided that the section entitled ``The GNU Manifesto''
is included exactly as in the original, and provided that
the entire resulting derived work is distributed under the terms of
a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, provided that the section entitled ``The GNU
Manifesto'' is included, either in English exactly
as in the original, or in a translation approved by the author.
@page
@headings on

@comment Here starts the actual text of the manual

@end iftex
@ifinfo
This file documents the GNU Emacs editor.  -*-Text-*-
Don't edit this file! It is produced by texinfo from another file.
Be sure to update the file's tag table after you generate a new version.

@node[Top,,, (DIR)]

The Emacs Editor

This is an INFO-ized Emacs reference manual.
@end ifinfo
@menu
* Intro::       An introduction to this documentation.
* Distrib::	How to get a copy of Emacs, or an Emacs manual.
* Glossary::    Definitions of important concepts, and cross refs.
* CommandIndex::Brief info on all commands ordered by topic,
                and cross refs.
* LibCat::      Brief info on available libraries.
* VarIndex::    Brief info on meanings of specific variables.
* Screen::      How to interpret what you see on the screen.
* Characters::  Emacs's character sets;  usage from deficient
                (ie, standard ASCII or upper case only) keyboards.
* Basic::       The most basic editing commands.

Important General Concepts
* Arguments::   Giving numeric arguments to commands.
* M-x::         Issuing long-named ``extended'' commands.
* Subsystems::  Commands that themselves read commands in a distinctive
                language, such as INFO and Backtrace.
* Recursive::   Recursive editing levels; situations when you are
                using ordinary Emacs commands but editing something
                special-purpose (such as a message to send).
* Exiting::     Exiting Emacs, subsystems, or recursive editing levels.
* Subforks::    Communicating with the operating-system:
                Running an inferior EXEC, checking for mail, etc.
* Help::        Commands for asking Emacs about its commands.

Important Text-Changing Commands
* Mark::        The mark: how to delimit a ``region'' of text.
* Killing::     Killing text.
* Un-killing::  Recovering killed text.  Moving text.
* Copying::     Other ways of copying text.
* Search::      Finding or replacing occurrences of a string.
* Text::        Commands and modes for editing English.
* Fixit::       Commands especially useful for fixing typos.
* Abbrev: (WORDAB),     How to define text abbreviations to reduce
                        the number of characters you must type.

Larger Units of Text
* Files::       All about handling files.
* Buffers::     Multiple buffers; editing several files at once.
* Display::     Controlling what text is displayed.
* Windows::     Viewing two pieces of text at once.
* Narrowing::   Commands for narrowing view to part of the buffer.
* Pages::       Commands for dealing with pages in files.
* Replace::     Repetitive search and replace commands.
* TECOSearch::  TECO search strings.

Editing Programs
* Major Modes:: Text mode vs. Lisp mode vs. MIDAS mode ...
* Programs::    Commands and modes for editing programs.
* Tags: (TAGS), The Tags subsystem remembers the location of each
                ``tag'' or function definition in one or more files,
                and allows you to go directly to any of them.

Customization
* Minor Modes:: Some useful options you can turn on and off.
* Libraries::   Loading additional libraries of commands.
* Variables::   Named variables:  what they do and how to use them.
* Syntax::      The syntax table.
* FS Flags::    FS flags: TECO's variables.
* Init::        Init files and Emacs.VARS files.
* Locals::      Local modes lists in files.
* KEYBOARD MACROS::      Making an abbreviation for a sequence of commands.
* Minibuffer::  Executing small TECO programs interactively.

Recovery from Lossage
* Quitting::    Quitting and Aborting.
* Lossage::     What to do if Emacs is hung.
* Undo::        Undoing a command that destroyed your text.
* Journals::    Journal files save all your commands in case of crash.
* Bugs::        How and when to report a bug.

Other Available Libraries
* PICTURE::     Subsystem for editing pictures made out of characters.
* Sort::        Commands for sorting part of the buffer.
* SLOWLY: (SLOWLY),     A package of macros for people using slow
                        terminals.
* RENUM:  (RENUM),      A package of macros for renumbering sections,
                        references, equations, etc. in manuscripts.
* DOCOND: (DOCOND),     Subsystem for ``assembly conditionals''
                        in documentation files.
* Babyl: (Babyl),        For reading mail.
* INTER: (INTER),       Interface between Emacs and Interlisp.
* LEDIT: (LEDIT),       Interface between Emacs and MacLisp.
* INFO: (INFO),         Subsystem for reading documentation files.
* CLU: (ECLU),          Subsystem containing CLU mode, a major mode.
* PL1: (EPL1),          Subsystem containing PL1 mode, a major mode.
* PASCAL: (EPASC),      Subsystem containing PASCAL mode, a major mode.
* TDEBUG: (TDEBUG),     Emacs macro 2 window real-time debugger.
* Internals: (CONV),    Emacs internals.  Customization.  Init files.
* TMACS: (TMACS)Top,    Assorted useful commands and subroutines.

Here are some other nodes which are really inferiors of the ones
already listed, so you can get to them in one step:

* Typeout::     Long messages that temporarily overwrite the text being edited.
* Echo Area::   Echoing of commands; brief messages, and error messages.
* Mode Line::   How to interpret the mode line at top level.
* MMArcana::    Hows and whys of MM commands.
* Mail::        Reading mail.
* Visiting::    How to visit a file for editing.
* ListDir::     How to list a directory.
* Revert::      How to cancel some changes you have made.
* AutoSave::    Protection from system crashes.
* CleanDir::    Deleting piled up old versions of files.
* Dired::       Deleting files by ``editing your directory''.
* Filadv::      Miscellaneous file commands.
* Indenting::   Indentation commands for programs.
* Matching::    Automatic display of how parens balance.
* Lisp::        Commands for Lisp code.
* Lists::       Commands for moving over lists.
* Defuns::      Commands for top level lists (defuns).
* Comments::    Commands that understand comments in code.
* Grinding::    Reformatting Lisp code.
* MIDAS::       Commands for assembler language code.
* Other Langs:: Programming languages other than Lisp and assembler.
* Words::       Commands for moving over words.
* Sentences::   Commands for sentences and paragraphs.
* TextIndent::  Commands for indenting text.
* Filling::     Commands for filling and centering text.
* Case::        Case conversion commands.
* NoLowerCase:: What to do on terminals with no lower case.
* Fonts::       Font change commands.
* Underlining:: Underlining commands.
* SCRIBE::      Editing SCRIBE input files.
* Dissociation::Dissociated Press.
* PAGE Lib: PAGE.       Macros for editing only one page at a time.
* Term Types::  How to specify the terminal type.
* Printing::    Printing terminals.
@end menu

@iftex
@unnumberedsec Preface
  This manual documents the use and simple customization of the
Emacs editor.  The reader is not expected to be a programmer.  Even simple
customizations do not require programming skill, but the user who is not
interested in customizing can ignore the scattered customization hints.

  This is primarily a reference manual, but can also be used as a
primer.  However, I recommend that the newcomer first use the on-line,
learn-by-doing tutorial, which you get by running Emacs and typing
@kbd{C-h t}.  With it, you learn Emacs by using Emacs on a specially
designed file which describes commands, tells you when to try them,
and then explains the results you see.  This gives a more vivid
introduction than a printed manual.

  On first reading, you need not make any attempt to memorize chapters one
and two, which describe the notational conventions of the manual and the
general appearance of the Emacs display screen.  It is enough to be aware
of what questions are answered in these chapters, so you can refer back
when you later become interested in the answers.  After reading chapter
four you should practice the commands there.  The next few chapters
describe fundamental techniques and concepts that are referred to again and
again.  It is best to understand them thoroughly, experimenting with them
if necessary.

  To find the documentation on a particular command, look in the
index.  Keys (character commands) and command names have separate
indexes just for them.  There is also a glossary, with a cross
reference for each term.

@ignore
  If you know vaguely what the command
does, look in the command summary.  The command summary contains a line or
two about each command, and a cross reference to the section of the
manual that describes the command in more detail; related commands
are grouped together.

  This manual comes in two forms: the published form and the Info
form.  The Info form is for on-line perusal with the INFO program;
it is distributed along with GNU Emacs.  Both forms contain
substantially the same text.
@end ignore

  GNU Emacs is a member of the Emacs editor family.  There are many Emacs
editors, all sharing common principles of organization.  For information on
the underlying philosophy of Emacs and the lessons learned from its
development, write for a copy of AI memo 519a, ``Emacs, the Extensible,
Customizable Self-Documenting Display Editor'', to

@display
Publications Department
Artificial Intelligence Lab
545 Tech Square
Cambridge, MA 02139
@end display
@end iftex
@comment from now on, things are mostly version-independent

@node[Intro, Distrib, Top, Top]

@unnumbered[Introduction]

  You are about to read about GNU Emacs, the Unix/GNU incarnation of the
advanced, self-documenting, customizable, extensible real-time display
editor Emacs.

  We say that Emacs is a @dfn{display} editor because normally the text
being edited is visible on the screen and is updated automatically as you
type your commands.  @xref[Screen,Display].

  We call it a @dfn{real-time} editor because the display is updated very
frequently, usually after each character or pair of characters you
type.  This minimizes the amount of information you must keep in your
head as you edit.  @xref[Basic,Real-time,Basic Editing].

  We call Emacs advanced because it provides facilities that go beyond
simple insertion and deletion: filling of text; automatic indentation of
programs; viewing two or more files at once; and dealing in terms of
characters, words, lines, sentences, paragraphs, and pages, as well as
expressions and comments in several different programming languages.  It is
much easier to type one command meaning ``go to the end of the paragraph''
than to find that spot with simple cursor keys.

  @dfn[Self-documenting] means that at any time you can type a special
character, @kbd{Control-h}, to find out what your options are.  You can
also use it to find out what any command does, or to find all the commands
that pertain to a topic.  @xref[Help].

  @dfn[Customizable] means that you can change the definitions of Emacs
commands in little ways.  For example, if you use a programming language in
which comments start with @samp[<**] and end with @samp[**>], you can tell
the Emacs comment manipulation commands to use those strings.  Another sort
of customization is rearrangement of the command set.  For example, if you
prefer the four basic cursor motion commands (up, down, left and right) on
keys in a diamond pattern on the keyboard, you can have it.
@xref[Customization].

  @dfn[Extensible] means that you can go beyond simple customization and
write entirely new commands, programs in the Lisp language.  Emacs is an
``on-line extensible'' system, which means that it is divided into many
functions that call each other, any of which can be redefined in the middle
of an editing session.  Any part of Emacs can be replaced without making a
separate copy of all of Emacs.  Most of the editing commands of Emacs are
written in Lisp already; the few exceptions could have been written in Lisp
but are written in C for efficiency.  Although only a programmer can write an
extension, anybody can use it afterward.

@node[Screen, Characters, VarIndex, Top]

@chapter The Organization of the Screen
@setref Screen
@cindex{screen}

  Emacs divides the screen into several areas, each of which contains
its own sorts of information.  The biggest area, of course, is the one
in which you usually see the text you are editing.

  When you are using Emacs, the screen is divided into a number of
@dfn{windows}.  Initially there is one text window occupying all but the
last line, plus the special @dfn{echo area} or @dfn{minibuffer window} in
the last line.  The text window can be subdivided horizontally or
vertically into multiple text windows, each of which can be used for a
different file (@pxref[Windows]).  The window that the cursor is in is the
@dfn{selected window}, in which editing takes place.  The other windows are
just for reference unless you select one of them.

  Each text window's last line is a @dfn{mode line} which describes what is
going on in that window.  It is in inverse video if the terminal supports that,
and contains text that starts like @samp{-----Emacs: @var[something]}.  Its purpose
is to indicate what buffer is being displayed in the window above
it; what major and minor modes are in use; and whether the buffer's text
has been changed.

@menu
* Point::	The place in the text where editing commands operate.
* Echo Area::   Short messages appear at the bottom of the screen.
* Mode Line::	Interpreting the mode line.
* Position Info:: Commands to print info on cursor position.
@end menu

@node[Point,Echo Area,Screen,Screen]

@section{Point}
@setref Point
@cindex{point}@cindex{cursor}

  When Emacs is running, the terminal's cursor shows the location at
which editing commands will take effect.  This location is called
@dfn{point}.  Other commands move point through the text, so that you
can edit at different places in it.

  While the cursor appears to point @var[at] a character, point should be
thought of as @var[between] two characters; it points @var[before] the
character that the cursor appears on top of.  Sometimes people speak of
``the cursor'' when they mean ``point'', or speak of commands that move
point as ``cursor motion'' commands.

  Terminals have only one cursor, and when output is in progress it must
appear where the typing is being done.  This does not mean that point is
moving.  It is only that Emacs has no way to show you the location of point
except when the terminal is idle.

  Each Emacs buffer has its own point location.  A buffer that is not being
displayed remembers where point is so that it can be seen when you look at
that buffer again.

  When there are multiple text windows, each window has its own point location.
The cursor shows the location of point in the selected window.  This also
is how you can tell which window is selected.  If the same buffer appears
in more than one window, point can be moved in each window independently.

  Point is called @dfn{dot} in the Emacs source code and on-line
documentation.  Both names come from the character @samp{.}, which was the
command in TECO (the language in which the original Emacs was written) for
accessing the value now called `point'.  The name `point' is preferred and
`dot' will being phased out.

@node[Echo Area,Mode Line,Point,Screen]

@section[The Echo Area]
@setref Echo Area
@cindex{echo area}

  The line at the bottom of the screen (below the mode line) is the
@dfn[echo area].  It is used to display small amounts of text for several
purposes.

  @dfn[Echoing] means printing out the characters that you type.  Emacs does
not echo single-character keys, and does not echo any keys if you type
the characters with no long pause, but if you pause for more than a second
in the middle of a multi-character key, then all the characters typed so
far are echoed.  This is intended to @dfn[prompt] you for the rest of the
key.  Once the beginning of a key has been echoed, all the rest is echoed
as soon as it is typed; so either the entire key or none of it is
echoed.  This behavior is designed to give confident users fast response,
while giving hesitant users maximum feedback.

  If a command cannot be executed, it may print an @dfn[error message] in
the echo area.  Error messages are accompanied by a beep or by flashing the
screen.  Also, any input you have typed ahead is thrown away when an error
happens.

  Some commands print informative messages in the echo area.  These
messages look much like error messages, but they are not announced with a
beep and do not throw away input.  Sometimes the message tells you what the
command has done, when it is not obvious from looking at the text being
edited.  Sometimes the sole purpose of a command is to print a message
giving you specific information.  For example, the command @kbd{C-x =} is
used to print a message describing the character position of point in the text
and its current column in the window.  Commands that take a long time
often display messages ending in @samp{@dots} while they are working, and
add @samp{done} at the end.

  The echo area is also used to display the @dfn[minibuffer], a window
that is used for reading arguments to commands, such as
the name of a file to be edited.  When the minibuffer is in use, the
echo area begins with a prompt string that ends with a colon; also,
the cursor appears in that line because it is the selected window.
You can always get out of the minibuffer by typing @kbd{C-g}.  @xref[Minibuffer].

@node[Mode Line,,Echo Area,Screen]

@section[The Mode Line]
@setref Mode Line
@cindex{mode line}
@cindex{top level}

  Each text window's last line is a @dfn{mode line} which describes what is
going on in that window.  When there is only one text window, the mode line
appears right above the echo area.  The mode line is in inverse video if
the terminal supports that, starts and ends with dashes, and contains text
like @samp{Emacs: @var[something]}.

  If a mode line has something else in place of @samp{Emacs:
@var[something]}, then the window above it is in a special subsystem such
as Rmail.  The mode line then indicates the status of the subsystem.

  Normally, the mode line has the following appearance:

@example
--@var{ch}-Emacs: @var[buf]      (@var[major] @var[minor])----@var[pos]%------
@end example

@nopara
This serves to indicate various information about the buffer being
displayed in the window: the buffer's name, what major and minor modes are
in use, whether the buffer's text has been changed, and how far down the
buffer you are currently looking.  The top level mode line has this format:

  @var[ch] contains two stars @samp{**} if the text in the buffer has been
edited (the buffer is``modified''), or @samp{--} if the buffer has not been
edited.  Exception: for a read-only buffer, it is @samp{%%}.

  @var[buf] is the name of the window's chosen @dfn[buffer].  The chosen
buffer in the selected window (the window that the cursor is in) is also
Emacs's selected buffer, the one that editing takes place in.  When we
speak of what some command does to ``the buffer'', we are talking about
the currently selected buffer.  @xref[Buffers].

  @var[major] is the name of the @dfn[major mode] in effect in the buffer.
At any time, each buffer is in one and only one of its possible major
modes.  The major modes available include Fundamental mode (the least
specialized), Text mode, Lisp mode, C mode, and others.  @xref[Major
Modes], for details of how the modes differ and how to select one.

  @var[minor] is a list of some of the @dfn[minor modes] that are turned on
at the moment in the window's chosen buffer.  @samp{Fill} means that Auto
Fill mode is on.  @samp{Abbrev} means that Word Abbrev mode is on.
@samp{Ovwrt} means that Overwrite mode is on.  @xref[Minor Modes], for more
information.  @samp{Narrow} means that the buffer being displayed has 
editing restricted to only a portion of its text.  This is not really
a minor mode, but is like one.  @xref[Narrowing].@refill
@c ??? Are these modes all buffer-specific?

  @var[pos] tells you whether there is additional text above the top of the
screen, or below the bottom.  If your file is small and it is all on the
screen, @var[pos] is @samp{All} is omitted.  Otherwise, it is @samp{Top} if
you are looking at the beginning of the file, @samp{Bot} if you are looking
at the end of the file, or @samp{@var[nn]%}, where @var[nn] is the
percentage of the file above the top of the screen.

  Some other information about the state of Emacs can also be displayed
among the minor modes.  @samp{Def} means that a keyboard macro is being
defined; although this is not exactly a minor mode, it is still useful to
be reminded about.  @xref[Keyboard Macros].

  In addition, if Emacs is currently inside a recursive editing level,
square brackets (@samp{[@dots]}) appear around the parentheses that
surround the modes.  If Emacs is in one recursive editing level within
another, double square brackets appear, and so on.  Since this information
pertains to Emacs in general and not to any one buffer, the square brackets
appear in every mode line on the screen or not in any of them.
@xref[Recursive Edit].

@section{Variables Controlling Display}
 
  This section contains information for customization only.  Beginning
users should skip it.

@vindex{mode-line-inverse-video}
  The variable @code{mode-line-inverse-video} controls whether the mode
line is displayed in inverse video (assuming the terminal supports it);
@code{nil} means don't do so.

@vindex{inverse-video}
  If the variable @code{inverse-video} is non-@code{nil}, Emacs attempts
to invert all the lines of the display from what they normally are.

@vindex{visible-bell}
  If the variable @code{visible-bell} is non-@code{nil}, Emacs attempts
to make the whole screen blink when it would normally make an audible bell
sound.  This variable has no effect if your terminal does not have a way
to make the screen blink.

@vindex{echo-keystrokes}
  The variable @code{echo-keystrokes} controls the echoing of
multi-character keys; its value is the number of seconds of pause required
to cause echoing to start, or zero meaning don't echo at all.  

@vindex{ctl-arrow}
@vindex{default-ctl-arrow}
  If the variable @code{ctl-arrow} is @code{nil}, control characters
also are displayed with octal escape sequences, all except @key(RET)
and @key(TAB).  This variable has a separate value in each buffer;
in new buffers, its value is initialized from the variable
@code{default-ctl-arrow}.

@vindex{tab-width}
@vindex{default-tab-width}
  Normally, a tab character in the buffer is displayed as whitespace which
extends to the next display tab stop position, and display tab stops come
at intervals equal to eight spaces.  The number of spaces per tab is
controlled by the variable @code{tab-width}, which is local to every
buffer just like @code{ctl-arrow} and gets its value in a new buffer from
@code{default-tab-width}.  Note that how the character tab in the
buffer is displayed has nothing to do with the definition of @key(TAB)
as a command.

@iftex
@chapter[Characters, Keys and Commands]

  This chapter explains the character set used by Emacs for input commands
and for the contents of files, and also explains the concepts of
@dfn[keys] and @dfn[commands] which are necessary for understanding how
your keyboard input is understood by Emacs.
@end iftex

@node[Characters,Entering Emacs,Screen,Top]

@section[The Emacs Character Set]
@setref Characters
@cindex{character set}
@cindex{ASCII}

  GNU Emacs uses the ASCII character set, which defines 128 different
character codes.  Some of these codes are assigned graphic symbols such
@samp{a} and @samp{=}; the rest are control characters, such as
@kbd{Control-a} (also called @kbd{C-a} for short).  @kbd{C-a} gets its name
from the fact that you type it by holding down the @key(CTRL) key and
then pressing @kbd{a}.  There is no distinction between @kbd{C-a} and
@kbd{C-A}; they are the same character.

  Some control characters have special names, and special keys you can
type them with: @key(RET), @key(TAB), @key(LFD), @key(DEL) and @key(ESC).
The space character is usually referred to below as @key(SPC), even though
strictly speaking it is a graphic character whose graphic happens to be
blank.

  Emacs extends the 7-bit ASCII code to an 8-bit code by adding an extra
bit to each character.  This makes 256 possible command characters.  The
additional bit is called Meta.  Any ASCII character can be made Meta; Meta
characters include @kbd{Meta-a} (@kbd{M-a}, for short), @kbd{M-A} (not the
same character as @kbd{M-a}, but those two characters normally have the
same meaning in Emacs), @kbd{M-@key(RET)}, and @kbd{M-C-a}.

@cindex{Control}@cindex{Meta}
@cindex{C-}@cindex{M-}
  Some terminals have a @key(META) key, and allow you to type Meta
characters by holding this key down.  Thus, @kbd{Meta-a} is typed by
holding down @key(META) and pressing @kbd{a}.  Such a key is not always
labeled @key(META), however, as it is usually a special option from the
manufacturer.  If there is no @key(META) key, you can still type Meta
characters using two-character sequences starting with @key(ESC).  Thus,
to enter @kbd{M-a}, you could type @kbd{@key(ESC) a}.  This is allowed
on terminals with Meta keys, too, in case you have formed a habit of doing
it.

@vindex{meta-flag}
  Emacs believes the terminal has a @key(META) key if the variable
@code{meta-flag} is non-@code{nil}.  Normally this is set automatically
according to the termcap entry for your terminal type.  However, sometimes
the termcap entry is wrong, and then it is useful to set this variable
yourself.

  Emacs buffers also use an 8-bit character set, because bytes have 8 bits,
but only the ASCII characters are considered meaningful.  ASCII graphic
characters in Emacs buffers are displayed with their graphics.
@key(LFD) is the same as a newline character; it is displayed by
starting a new line.  @key(TAB) is displayed by moving to the next tab stop
column (usually every 8 columns).  Other control characters are displayed
as a caret (@samp{^}) followed by the non-control version of the character;
thus, @kbd{C-a} is displayed as @samp{^A}.  Non-ASCII characters 128 and up
are displayed with octal escape sequences; thus, character code 243
(octal), also called @kbd{M-#} when used as an input character, is
displayed as @samp{\243}.

@section[Keys]
@setref Keys

@cindex{key}
  A @dfn{key}---short for @dfn{key sequence}---is a sequence of characters
that is all part of specifying a single Emacs command to be run.  If the
characters are enough to specify a command, they form a @dfn{complete key}.

@kindex{C-x}
@kindex{C-h}
@kindex{ESC}
  A single character is always a key; whether it is complete depends on its
meaning in Emacs.  Most single characters are complete Emacs commands.
@kbd{C-h}, @kbd{C-x} and @key(ESC) are the only ones that are not
complete.

@cindex{prefix key}
  A sequence of characters that is not enough to specify an Emacs command
is called a @dfn{prefix key}.  A prefix key is the beginning of a series of
longer sequences that are valid keys; adding any single character to the
end of the prefix gives a valid key, which could be defined as an Emacs
command.  For example, @kbd{C-x} is normally defined as a prefix, so
@kbd{C-x} and the next input character combine to make a two-character key.
There are 256 different two-character keys starting with @kbd{C-x}, one for
each possible second character.  Most of these two-character keys starting
with @kbd{C-x} are standardly defined as Emacs commands.  The most notable
ones are @kbd{C-x C-f} or @kbd{C-x s} (@pxref[Files]).

  Adding one character to a prefix key does not have to form a complete
key.  It could make another, longer prefix.  For example, @kbd{C-x 4} is
itself a prefix that leads to 256 different three-character keys, including
@kbd{C-x 4 f}, @kbd{C-x 4 b} and so on.  It would be possible to define one
of those three-character sequences as a prefix, creating a series of
four-character keys, but we did not define any of them this way.

  All told, the prefix keys in Emacs are @kbd{C-x}, @kbd{C-h}, @kbd{C-x 4},
and @key(ESC).  @kbd{C-c} is an additional prefix but only in certain
modes.

@section[Keys and Commands]
@setref Commands

@cindex{binding}
@cindex{customization}
@cindex{keymap}
@cindex{function}
@cindex{command}
  This manual is full of passages that tell you what particular keys do.
But Emacs does not assign meanings to keys directly.  Instead, Emacs
assigns meanings to @dfn[functions], and then gives keys their meanings by
@dfn[binding] them to functions. 

  A function is a Lisp object that can be executed as a program.
Usually it is a Lisp symbol which has been given a function definition;
every symbol has a name, usually made of a few English words separated by
dashes, such as @code{next-line} or @code{forward-word}.  It also has a
@dfn{definition} which is a Lisp program; this is what makes the function
do what it does.  Only some functions can be the bindings of keys; these
are functions whose definitions use @code{interactive} to specify how to
call them interactively.  Such functions are called @dfn{commands}, and
the name of a symbol that is a command is called a @dfn{command name}.
More information on this subject will appear in the @i{GNU Emacs Lisp
Manual} (which is not yet written).

  The bindings between keys and functions are recorded in various tables
called @dfn[keymaps].  @xref[Keymaps].

  When we say that ``@kbd[C-n] moves down vertically one line'' we are
glossing over a distinction that is irrelevant in ordinary use but is vital
in understanding how to customize Emacs.  It is the function
@code{next-line} that is programmed to move down vertically.  @kbd[C-n] has
this effect @i[because] it is bound to that function.  If you rebind
@kbd[C-n] to the function @code[forward-word] then @kbd[C-n] will move
forward by words instead.  Rebinding command characters is a common
method of customization.

  In the rest of this manual, we usually ignore this subtlety to keep
things simple.  To give the customizer the information he needs, we
state the name of the command which really does the work in parentheses
after mentioning the key that runs it.  For example, we will say that
``The command @kbd{C-n} (@code{next-line}) moves point vertically down,''
meaning that @code{next-line} is a command that moves vertically down
and @kbd{C-n} is a key that is standardly bound to it.

@cindex{variables}
  While we are on the subject of customization information which you should
not be frightened of, it's a good time to tell you about @dfn[variables].
Often the description of a command will say, ``To change this, set the
variable @code{mumble-foo}.''  A variable is a name used to remember a
value.  Most of the variables documented in this manual exist just to
permit customization: the variable's value is examined by some command,
and changing the value makes the command behave differently.  Until you
are interested in customizing,  you can ignore this information.  When you
are ready to be interested, readthe basic information on variables, and
then the information on individual variables will make sense.
@xref[Variables].

@node[Entering Emacs,Exiting,Characters,Top]

@chapter[Entering and Exiting Emacs]
@setref Entering Emacs
@cindex{entering Emacs}
@cindex{arguments (from shell)}

  The simplest way to invoke Emacs is just to type @kbd{emacs
@key(RET)} at the shell.

  It is also possible to specify files to be visited, Lisp files to be
loaded, and functions to be called, using by giving Emacs arguments in
the shell command line.  Here are the arguments allowed:

@table 7
@item -q
Do not load your Emacs init file @code{~/.emacs}.

@item -u @var[user]
Load @var[user]'s Emacs init file @code{~@var[user]/.emacs} instead of
your own.

@item -t @var[device]
Use @var[device] as the terminal for editing input and output.

@samp{-t} is recognized only if it is the first command argument.

@cindex{batch mode}
@item -batch
Run Emacs in @dfn{batch mode}, which means that the text being
edited is not displayed and the standard Unix interrupt characters
@kbd{C-z}, @kbd{C-c} and @kbd{C-\} continue to have their normal
effect.  Emacs in batch mode outputs to @code{stdout} only what would
normally be printed in the echo area under program control.

Batch mode is used for running programs written in Emacs Lisp
from shell scripts, makefiles, and so on.

@samp{-batch} is recognized only if it is the first command argument.

@item -l @var[file]
Load a file @var[file] of Lisp code with @code{load}.  @xref[Lisp
Libraries].

@item -f @var[function]
Call Lisp function @var[function] with no arguments.

@item -kill
Exit from Emacs without asking for confirmation.

@item @var[file]
Visit @var[file] using @code{find-file}.  @xref[Visiting].

@item +@var[linenum] @var[file]
Visit @var[file] using @code{find-file}, then go to line number
@var[linenum] in it.
@end table

  The command arguments are processed in the order they appear in the
command argument list; however, certain switches must be at the front
of the list (@samp{-t} or @samp{-batch}) if they are used.

  One way to use this is to visit many files automatically:
@example
emacs *.c
@end example
@nopara
which passes each @code{.c} file as a separate argument to Emacs,
so that Emacs visits each one.
  
  Here is an advanced example that assumes you have a Lisp program
file called @code{hack-c-program.el} which, when loaded, performs some
useful operation on current buffer, expected to be a C program.
@example
emacs -batch foo.c -l hack-c-program -f save-buffer -kill-emacs > log
@end example
@nopara
Here Emacs is told to visit @code{foo.c}, load @code{hack-c-program.el}
(which makes changes in the visited file), save @code{foo.c}
(note that @code{save-buffer} is the function that @kbd{C-x C-s}
is bound to), and then exit to the shell that this command was
done with.  @samp{-batch} guarantees there will be no problem
redirecting output to @code{log}, because Emacs will not assume that
it has a display terminal to work with.

@node[Exiting,Basic,Entering Emacs,Top]

@section[Exiting Emacs]
@cindex{exiting}
@cindex{killing Emacs}
@cindex{suspending}

  There are two commands for exiting Emacs because there are two kinds of
exiting: @dfn{suspending} Emacs and @dfn{killing} Emacs.  @dfn{Suspending}
means stopping Emacs temporarily and returning control to its superior
(usually the shell), allowing you to resume editing later in the same
Emacs job, with the same files, same kill ring, same undo history, and
so on.  This is the usual way to exit.  @dfn{Killing} Emacs means destroying
the Emacs job.  You can run Emacs again after killing it, but you will get
a fresh Emacs; there is no way to resume the same editing session after
it has been killed.

@kindex{C-z}
@cfindex{suspend-emacs}
  To suspend Emacs, type @kbd{C-z} (@code{suspend-emacs}).

@kindex{C-x C-c}
@cfindex{save-buffers-kill-emacs}
  To kill Emacs, type @kbd{C-x C-c} (@code{save-buffers-kill-emacs}).
A two-character key is used for this to make it harder to type.
Unless a numeric argument is used, this command first offers to save
any modified buffers.  If you do not save them all, it asks for
reconfirmation with `yes' before killing Emacs, since any changes not
saved before that will be lost forever.  Also, if any subprocesses are
still running, @kbd{C-x C-c} asks for confirmation about them, since
killing Emacs will kill the subprocesses immediately.

  In most Unix programs, @b{but not in Emacs}, the characters
@kbd{C-z} and @kbd{C-c} instantly suspend or kill, respectively.  The
meanings of @kbd{C-z} and @kbd{C-x C-c} as keys in Emacs were inspired by
the standard Unix meanings of @kbd{C-z} and @kbd{C-c}, but there is no
causal connection.  The standard Unix handling of @kbd{C-z} and
@kbd{C-c} are turned off in Emacs.  You could customize these
keys to do anything (@pxref[Keymaps]).

@node[Basic,Arguments,Exiting,Top]

@chapter[Basic Editing Commands]
@setref Basic

@kindex{C-h t}
@cfindex{help-with-tutorial}
  We now give the basics of how to enter text, make corrections, and
save the text in a file.  If this material is new to you, you might
learn it more easily by running the Emacs learn-by-doing tutorial.  To
do this, type @kbd{Control-h t} (@code{help-with-tutorial}).

@section[Inserting Text]

@cindex{insertion}
@cindex{point}
@cindex{cursor}
@cindex{graphic characters}
  To insert printing characters into the text you are editing, just
type them.  Except in special modes, Emacs defines each printing
character as a key to run the command @code{self-insert}, which inserts
the character that you typed to invoke it into the buffer at the
cursor (that is, at @dfn[point]; @pxref[Point]).  The cursor moves
forward.  Any characters after the cursor move forward too.  If the
text in the buffer is @samp[FOOBAR], with the cursor before the
@samp[B], then if you type @kbd[XX], you get @samp[FOOXXBAR], with the
cursor still before the @samp[B].

@kindex{DEL}
@cindex{deletion}
@cfindex{delete-backward-char}
   To @dfn{delete} text you have just inserted, you can use @key(DEL)
(which runs the command named @code{delete-backward-char}).
@key(DEL) deletes the character @var[before] the cursor (not the
one that the cursor is on top of or under; that is the character
@var[after] the cursor).  The cursor and all characters after it move
backwards.  Therefore, if you type a printing character and then type
@key(DEL), they cancel out.

@kindex{RET}
@cfindex{newline}
@cindex{newline}
   To end a line and start typing a new one, type @key(RET)
(running the command @code[newline]).  @key(RET) operates
by inserting a newline character in the buffer.  If point is in the
middle of a line, @key(RET) splits the line.  Typing @key(DEL)
when the cursor is at the beginning of a line rubs out the newline
before the line, thus joining the line with the preceding line.

@cindex{quoting}
@kindex{C-q}
@cfindex{quoted-insert}
  Direct insertion works for printing characters and @key(SPC), but other
characters act as editing commands and do not insert themselves.  If
you need to insert a control character or a character code above 200
octal, you must @dfn{quote} it by typing @kbd{Control-q}
(@code{quoted-insert}) first.  There are two ways to use @kbd{C-q}:

@itemize @bullet
@item
@kbd{Control-q} followed by any non-graphic character (even @kbd(C-g))
inserts that character.
@item
@kbd{Control-q} followed by three octal digits inserts the character
with the specified character code.
@end itemize
@nopara
A numeric argument to @kbd{C-q} specifies how many copies of the
quoted character should be inserted (@pxref[Arguments]).

@section{Continuation Lines}
@setref Continuation Lines

@cindex{continuation line}
  If you add too many characters to one line, without breaking it with
a @key(RET), the line will grow to occupy two (or more) lines on
the screen, with a @samp[\] at the extreme right margin of all but the
last of them.  The @samp[\] says that the following screen line is not
really a distinct line in the text, but just the @dfn[continuation] of
a line too long to fit the screen.  Sometimes it is nice to have Emacs
insert newlines automatically when a line gets too long; for this, use
Auto Fill mode (@pxref[Filling]).

@vindex{truncate-lines}
@vindex{default-truncate-lines}
@cindex{truncation}
  Continuation can be turned off for a particular buffer by setting the
variable @code{truncate-lines} to non-@code{nil} in that buffer.  Then,
lines are @dfn{truncated}: the text that goes past the right margin does
not appear at all.  @samp{$} is used in the last column instead of @samp{\}
when truncation is in effect.  Truncation instead of continuation also
happens whenever horizontal scrolling is in use, and optionally whenever
side-by-side windows are in use (@pxref[Windows]).  @code{truncate-lines}
is automatically local in all buffers.  When a buffer is created, its value
of @code{trucate-lines} is initialized from the value of
@code{default-truncate-lines}, normally @code{nil}.

@section[Changing the Location of Point]

  To do more than insert characters, you have to know how to move
point (@pxref[Point]).  Here are a few of the commands for doing that.

@kindex{C-a}
@kindex{C-e}
@kindex{C-f}
@kindex{C-b}
@kindex{C-n}
@kindex{C-p}
@kindex{C-l}
@kindex{C-t}
@kindex{M->}
@kindex{M-<}
@cfindex{beginning-of-line}
@cfindex{end-of-line}
@cfindex{forward-char}
@cfindex{backward-char}
@cfindex{next-line}
@cfindex{previous-line}
@cfindex{recenter}
@cfindex{transpose-chars}
@cfindex{beginning-of-buffer}
@cfindex{end-of-buffer}
@cfindex{goto-char}
@table 7
@item C-a
Move to the beginning of the line (@code{beginning-of-line}).
@item C-e
Move to the end of the line (@code{end-of-line}).
@item C-f
Move forward one character (@code{forward-char}).
@item C-b
Move backward one character (@code{backward-char}).
@item C-n
Move down one line, vertically (@code{next-line}).  If you start in the
middle of one line, you end in the middle of the next.
From the last line of text, @kbd{C-n} creates a new line and moves
onto it.
@item C-p
Move up one line, vertically (@code{previous-line}).
@item C-l
Clear the screen and reprint everything (@code{recenter}).
@item C-t
Transpose two characters, the ones before and after the cursor
(@code{transpose-chars}).
@item M-<
Move to the top of the buffer (@code{beginning-of-buffer}).
With numeric argument @var{n}, move to
@var{n}/10 of the way from the top.  @xref[Arguments], for more information
on numeric arguments.
@item M->
Move to the end of the buffer (@code{end-of-buffer}).
@item M-x goto-char
Read a number @var{n} and move cursor to character number @var[n].
Position 1 is the beginning of the buffer.
@item M-x goto-line
Read a number @var{n} and move cursor to line number @var[n].
Line 1 is the beginning of the buffer.
@item C-x C-n
Set current column as goal column for @kbd{C-n} and @kbd{C-p}.
Henceforth, those commands move to this fixed column in the line moved to
(@code{set-goal-column}).
@item C-u C-x C-n
Cancel the goal column.  Henceforth, @kbd{C-n} and @kbd{C-p}
try to stay in the same column, as usual.
@end table

@vindex{track-eol}
  If you set the variable @code{track-eol} to a non-@code{nil} value, then
@kbd{C-n} and @kbd{C-p} when at the end of the starting line move to the
end of the line.  Normally, @code{track-eol} is @code{nil}.

@section[Erasing Text]

@table 7
@item @key(DEL)
Delete the character before the cursor (@code{delete-backward-char}).
@item C-d
Delete the character after the cursor (@code{delete-forward-char}).
@item C-k
Kill to the end of the line (@code{kill-line}).
@end table

  You already know about the @key(DEL) key which deletes the
character before the cursor.  Another key, @kbd[Control-d], deletes the
character after the cursor, causing the rest of the text on the line to
shift left.  If @kbd[Control-d] is typed at the end of a line, that line
and the next line are joined together.

  To erase a larger amount of text, use the @kbd[Control-k] key, which
kills a line at a time.  If @kbd[Control-k] is done at the beginning or
middle of a line, it kills all the text up to the end of the line.  If
@kbd[Control-k] is done at the end of a line, it joins that line and the
next line.

  @xref[Killing], for more flexible ways of killing text.

@section[Files]

@cindex{files}
  The commands above are sufficient for creating and altering text in an
Emacs buffer; the more advanced Emacs commands just make things easier.
But to keep any text permanently you must put it in a @dfn[file].  Files
are named units of text which are stored by the operating system for you to
retrieve later by name.  To look at or use the contents of a file in any
way, including editing the file with Emacs, you must specify the file name.

  Consider a file named @code{/usr/rms/foo.c}.  To edit this file in Emacs,
type
@example
C-x C-f /usr/rms/foo.c @key(RET)
@end example
@nopara
Here the file name is given as an @dfn{argument} to the command @kbd{C-x
C-f} (@code{find-file}).  @key(RET) is used to terminate the argument.
Emacs obeys the command by @dfn{visiting} the file: creating a buffer,
copying the contents of the file into the buffer, and then displaying the
buffer for you to edit.  You can make changes in it, and then @dfn[save]
the file by typing @kbd{C-x C-s} (@code{save-buffer}).  This makes the
changes permanent by copying the altered contents of the buffer back into
the file @code{/usr/rms/foo.c}.  Until then, the changes are only inside
your Emacs, and the file @code{foo.c} is not changed.@refill

  To create a file, just visit the file with @kbd{C-x C-f} as if it already
existed.  Emacs will make an empty buffer in which you can insert the text
you want to put in the file.  When you save your text with @kbd{C-x C-s},
the file will be created.

  Of course, there is a lot more to learn about using files.  @xref[Files].

@section[Help]

  If you forget what a key does, you can find out with the Help
character, which is @kbd{C-h}.  Type @kbd{C-h k} followed by the key
you want to know about; for example, @kbd{C-h k C-n} tells you all
about what @kbd{C-n} does.  @kbd{C-h} is a prefix key; @kbd{C-h k} is
just one of its subcommands.  The other subcommands of @kbd{C-h} provide
different kinds of help.  @xref[Help].

@section[Blank Lines]
@setref Blank Lines

@c widecommands
@table 7
@item C-o
Insert one or more blank lines after the cursor.
@item C-x C-o
Delete all but one of many consecutive blank lines.
@end table

@kindex{C-o}
@kindex{C-x C-o}
@cindex{blank lines}
@cfindex{open-line}
@cfindex{delete-blank-lines}
  When you want to insert a new line of text before an existing line, you
can do it by typing the new line of text, followed by @key(RET).
However, it may be easier to see what you are doing if you first make a
blank line and then insert the desired text into it.  This is easy to do
using the key @kbd{C-o} (Customizers: this is bound to the command
@code{open-line}), which inserts a newline after point but leaves point in
front of the newline.  After @kbd{C-o}, type the text for the new line.
@kbd{C-o F O O} has the same effect as @kbd{F O O @key(RET)}, except for
the final location of point.

  You can make several blank lines by typing @kbd{C-o} several times, or by
giving it an argument to tell it how many blank lines to make.
@xref[Arguments], for how.

  If you have many blank lines in a row and want to get rid of them, use
@kbd{C-x C-o} (the command @code{delete-blank-lines}).  When point is on a
blank line which is adjacent to at least one other blank line, @kbd{C-x
C-o} deletes all but one of the consecutive blank lines, leaving exactly
one.  With point on a blank line with no other blank line adjacent to it,
the sole blank line is deleted, leaving none.  When point is on a nonblank
line, @kbd{C-x C-o} deletes any blank lines following that nonblank line.

@section[Cursor Position Information]
@setref Position Info

  If you are accustomed to other display editors, you may be surprised that
Emacs does not always display the page number or line number of point in
the mode line.  This is because the text is stored in a way that makes it
difficult to compute this information.  Displaying them all the time would
be intolerably slow.  They are not needed very often in Emacs anyway,
but there are commands to print them.

@table 7
@item C-x =
Print character code of character after point, character position of point,
and column of point (@code{what-cursor-position}).
@item M-x what-page
Print page number of point, and line number within page.
@item M-x what-line
Print line number of point in the buffer.
@item M-=
Print number of lines in the current region.
@end table

@kindex{C-x =}
@cfindex{what-cursor-position}
  The command @kbd{C-x =} (@code{what-cursor-position}) can be used to find out
the column that the cursor is in, and other miscellaneous information about
point.  It prints a line in the echo area that looks like this:
@example
Char: x (170)  dot=24182 of 469463(5%)  x=40
@end example
@nopara
(In fact, this is the output produced when point is before the @samp{x=40}
in the example.)

  The two values after @samp{Char:} are describe the character following point,
first by showing it and second by giving its octal character code.

  @samp{dot=} is followed by the position of point expressed as a character count.
The front of the buffer counts as position 1, one character later as 2, and so on.
The next, larger number is the total number of characters in the buffer.
Afterward in parentheses comes the position expressed as a percentage of the
total size.

  @samp{x=} is followed by the horizontal position of point, in columns from the
left edge of the window.

  If the buffer has been narrowed, making some of the text at the beginning and
the end temporarily invisible, @kbd{C-x =} prints additional text describing the
current visible range.  For example, it might say
@example
Char: x (170)  dot=24182 of 469463(5%) <23868 - 25074>  x=40
@end example
@nopara
where the two extra numbers give the smallest and largest character position
that point is allowed to assume.  The characters between those two positions
are the visible ones.  @xref[Narrowing].

  If point is at the end of the buffer (or the end of the visible part),
@kbd{C-x =} omits any description of the character after point.
The output looks like
@example
dot=469463 of 469463(5%)  x=0
@end example
@nopara
Usually @samp{x=0} at the end, because the text usually ends with a newline.

@cfindex{what-page}
@cfindex{what-line}
  There are two commands for printing line numbers.  @kbd{M-x what-line}
counts lines from the beginning of the file and prints the line number
point is on.  The first line of the file is line number 1.  By contrast,
@kbd{M-x what-page} counts pages from the beginning of the file, and
counts lines within the page, printing both of them.  @xref[Pages].

@kindex{M-=}
@cfindex{count-lines-region}
  While on this subject, we might as well mention @kbd{M-=}
(@code{count-lines-region}), which prints the number of lines in the region
(@pxref[Mark]), since there is no other obvious place to stick it.
@xref[Pages], for the command @kbd{C-x l} which counts the lines in the
current page.

@node[Arguments,Minibuffer,Basic,Top]

@chapter[Numeric Arguments]
@setref Arguments
@cindex{numeric arguments}

  Any Emacs command can be given a @dfn[numeric argument].  Some commands
interpret the argument as a repetition count.  For example, giving an
argument of ten to the key @kbd{C-f} (the command
@code{forward-char}, move forward one character) moves forward ten
characters.  With these commands, no argument is equivalent to an
argument of one.  Negative arguments are allowed.  Often they tell
a command to move or act backwards.

  Some commands care only about whether there is an argument, and not about
its value.  For example, the command @kbd{M-q} (@code{fill-paragraph}) with
no argument fills text; with an argument, it justifies the text as well.
(@xref[Filling], for more information on @kbd{M-q}.)

  Some commands use the value of the argument as a repeat count, but do
something peculiar when there is no argument.  For example, the command
@kbd{C-k} (@code{kill-line}) with argument @var{n} kills @var{n} lines,
including their terminating newlines.  But @kbd{C-k} with no argument is
special: it kills the text up to the next newline, or, if point is right at
the end of the line, it kills the newline itself.  Thus, two @kbd{C-k}
commands with no arguments can kill a nonblank line, just like @kbd{C-k}
with an argument of one.  (@xref[Killing], for more information on
@kbd{C-k}.)

@kindex{M-1}
@kindex{M--}
@cfindex{digit-argument}
@cfindex{negative-argument}
  If your terminal keyboard has a @key(META) key, the easiest way to
specify a numeric argument is to type digits and/or a minus sign while
holding down the the @key(META) key.  For example,
@example
M-5 C-n
@end example
@nopara
would move down five lines.  The characters @kbd{Meta-1},
@kbd{Meta-2}, etc., and @kbd{Meta--}, do this because they are keys
bound to commands (@code{digit-argument} and @code{negative-argument})
that are defined to contribute to an argument for the next command.

@kindex{C-u}@cfindex{universal-argument}
  Another way of specifying an argument is to use the @kbd{C-u}
(@code{universal-argument}) command followed by the digits of the argument.
With @kbd{C-u}, you can type the argument digits without holding
down shift keys.  To type a negative argument, start with a minus sign.
Just a minus sign normally means -1.  @kbd{C-u} works on all terminals.

  @kbd{C-u} followed by a character which is neither a digit nor a minus
sign has the special meaning of ``multiply by four''.  It multiplies the
argument for the next command by four.  @kbd{C-u} twice multiplies it by
sixteen.  Thus, @kbd{C-u C-u C-f} moves forward sixteen characters.  This
is a good way to move forward ``fast'', since it moves about 1/5 of a line
in the usual size window and font.  Other useful combinations are @kbd{C-u
C-n}, @kbd{C-u C-u C-n} (move down a good fraction of a screen), @kbd{C-u
C-u C-o} (make ``a lot'' of blank lines), and @kbd{C-u C-k} (kill four
lines).  With commands like @kbd{M-q} that care whether there is an
argument but not what the value is, @kbd{C-u} is a good way of saying,
``Let there be an argument.''

  A few commands treat a plain @kbd{C-u} differently from an ordinary
argument.  A few others may treat an argument of just a minus sign
differently from an argument of -1.  These unusual cases will be described
when they come up; they are always for reasons of convenience of use of the
individual command.

@c section[Autoarg Mode]
@ignore
@cindex{autoarg mode}
  Users of ASCII keyboards may prefer to use Autoarg mode.  Autoarg mode
means that you don't need to type C-U to specify a numeric argument.
Instead, you type just the digits.  Digits followed by an ordinary
inserting character are themselves inserted, but digits followed by an
Escape or Control character serve as an argument to it and are not
inserted.  A minus sign can also be part of an argument, but only at the
beginning.  If you type a minus sign following some digits, both the digits
and the minus sign are inserted.

  To use Autoarg mode, set the variable Autoarg Mode nonzero.
@xref[Variables].

  Autoargument digits echo at the bottom of the screen; the first nondigit
causes them to be inserted or uses them as an argument.  To insert some
digits and nothing else, you must follow them with a Space and then rub it
out.  C-G cancels the digits, while Delete inserts them all and then rubs
out the last.
@end ignore

@node[Minibuffer,M-x,Arguments,Top]

@chapter[The Minibuffer]
@setref Minibuffer
@cindex{minibuffer}

  The @dfn[minibuffer] is the facility used by Emacs commands to read
arguments more complicated than a single number.  Minibuffer arguments can
be file names, buffer names, Lisp function names, Emacs command
names, Lisp expressions, and many other things, depending on the command
reading the argument.

@cindex{prompt}
  When the minibuffer is in use, it appears in the echo area, and the
terminal's cursor moves there.  The beginning of the minibuffer line
displays a @dfn{prompt} which says what kind of input you should supply and
how it will be used.  Often this prompt is derived from the name of the
command that the argument is for.  The prompt normally ends with a colon.

@cindex{default argument}
  Sometimes a @dfn{default argument} appears in parentheses after the
colon; it too is part of the prompt.  The default will be used as the
argument value if you enter an empty argument (e.g., just type
@key(RET)).  For example, commands that read buffer names always show
a default, which is the name of the buffer that will be used if you type
just @key(RET).

@kindex{C-g}
  The simplest way to give a minibuffer argument is to type the text you
want, terminated by @key(RET) which exits the minibuffer.  You can
get out of the minibuffer, canceling the command that it was for, by
typing @kbd{C-g}.

  Since the minibuffer uses the screen space of the echo area, it can
conflict with other ways Emacs customarily uses the echo area.  Here is
how Emacs handles such conflicts:

@itemize @bullet
@item
If a command gets an error while you are in the minibuffer, this does
not cancel the minibuffer.  However, the echo area is needed for the
error message and therefore the minibuffer itself is hidden for a while.
It comes back after a few seconds.

@item
If in the minibuffer you use a command whose purpose is to print a
message in the echo area, such as @kbd{C-x =}, the message is printed
normally, and the minibuffer is hidden until the next time you type
a character.

@item
Echoing of commands does not take place while the minibuffer is in use.
@end itemize

@menu
* File: Minibuffer File Names.  Entering file names with the minibuffer.
* Edit: Minibuffer Edit.	How to edit in the minibuffer.
* Completion::		An abbreviation facility for minibuffer input.
* Repetition::		Re-executing previous commands that used the minibuffer.
@end menu

@node[Minibuffer File,Minibuffer Edit,Minibuffer,Minibuffer]

@section{Minibuffers for File Names}
@setref Minibuffer File

  Sometimes the minibuffer starts out with text in it.  For example, when
you are supposed to give a file name, the minibuffer starts out containing
the @dfn{default directory}, which ends with a slash.  This is to inform
you which directory the file will be found in if you do not specify a
directory.  For example, the minibuffer might start out with
@example
Find File: /u2/emacs/src/
@end example
@nopara
where @samp{Find File: } is the prompt.  Typing @kbd{buffer.c}
specifies the file @code{/u2/emacs/src/buffer.c}.  To find files in nearby
directories, use @kbd{..}; thus, if you type @kbd{../lisp/simple.el}, you
will find the file @code{/u2/emacs/lisp/simple.el}.  Alternatively,
you can kill with @kbd{M-@key(DEL)} the directory names you don't want.

  You can also type an absolute file name, one starting with a slash or a
tilde, ignoring the default directory.  For example, to find the file
@code{/etc/termcap}, just type the name, giving
@example
Find File: /u2/emacs/src//etc/termcap
@end example
@nopara
Two slashes in a row are not normally meaningful in Unix file names, but
they are allowed in GNU Emacs.  They mean, ``ignore everything before the
second slash in the pair.''  Thus, @samp{/u2/emacs/src/} is ignored, and
you get the file @code{/etc/termcap}.

@vindex{insert-default-directory}
  If you set @code{insert-default-directory} to @code{nil}, the default
directory is not inserted in the minibuffer.

@node[Minibuffer Edit,Completion,Minibuffer File,Minibuffer]

@section{Editing in the Minibuffer}
@setref Minibuffer Edit

  The minibuffer is an Emacs buffer (albeit a peculiar one), and the usual
Emacs commands are available for editing the text of an argument you are
entering.

  Since @key(RET) in the minibuffer is defined to exit the
minibuffer, inserting a newline into the minibuffer must be done with
@kbd{C-o} or with @kbd{C-q @key(LFD)}.  (Recall that a newline is
really the @key(LFD) character.)

  The minibuffer has its own window which always has space on the
screen but acts as if it were not there when the minibuffer is not in use.
When the minibuffer is in use, its window is just like the others; you can
switch to another window with @kbd{C-x o}, edit text in other windows and
perhaps even visit more files, before returning to the minibuffer to submit
the argument.  You can kill text in another window, return to the
minibuffer window, and then yank the text to use it in the argument.
@xref[Windows].

  There are some restrictions on the use of the minibuffer window, however.
You cannot switch buffers in it---the minibuffer and its window are
permanently attached.  Also, you cannot split the minibuffer window.

  Recursive use of the minibuffer is supported by Emacs.  However, it is
easy to do this by accident (because of autorepeating keyboards, for
example) and get confused.  Therefore, most Emacs commands that use the
minibuffer refuse to operate if the minibuffer window is selected.
If the minibuffer is active but you have switched to a different window,
recursive use of the minibuffer is allowed---if you know enough to try to
do this, you probably will not get confused.

@vindex{enable-recursive-minibuffers}
  If you set the variable @code{enable-recursive-minibuffers} to be
non-@code{nil}, recursive use of the minibuffer is always allowed.

@node[Completion,Repetition,Minibuffer Edit,Minibuffer]

@section{Completion}
@setref Completion
@cindex{completion}

  Often, the minibuffer provides a @dfn{completion} facility.  This means that you
type enough of the argument to determine the rest, based on Emacs's knowledge of
which arguments make sense, and Emacs visibly fills in the rest, or as much as can
be determined from the part you have typed.

  When completion is available, certain keys---@key(TAB), @key(RET),
and @key(SPC)---are redefined to complete an abbreviation present in the
minibuffer into a longer string that it stands for, by matching it against
a set of @dfn{completion alternatives} provided by the command reading the
argument.

  For example, when the minibuffer is being used by @kbd{Meta-x} to
read the name of a command, it is given a list of all available
Emacs command names to complete against.  The completion keys
match the text in the minibuffer against all the command names, find
any additional characters of the name that are implied by the ones
already present in the minibuffer, and add those characters to the ones
you have given.

  Here is a list of all the completion commands, defined in the
minibuffer when completion is available.

@table 7
@item @key(TAB)
Complete the text in the minibuffer as much as possible
(@code{minibuffer-complete}).
@item @key(SPC)
Complete the text in the minibuffer but don't add or fill out more than
one word (@code{minibuffer-complete-word}).
@item @key(RET)
Submit the text in the minibuffer as the argument, possibly completing first
as described below (@code{minibuffer-complete-and-exit}).
@item ?
Print a list of all possible completions of the text in the minibuffer
(@code{minibuffer-list-completions}).
@end table

@kindex{TAB}
@cfindex{minibuffer-complete}
  Completion is very hard to explain but easy to understand once you have
seen it in operation.  If you type @kbd{Meta-x au @key(TAB)}, the @key(TAB)
looks for alternatives (in this case, command names) that start with
@samp{au}.  In this case, there are only two: @code{auto-fill-mode} and
@code{auto-save-mode}.  These are the same as far as @code{auto-}, so the
@samp{au} in the minibuffer changes to @samp{auto-}.

  If you go on to type @kbd{f @key(TAB)}, this second @key(TAB) sees
@samp{auto-f}.  The only command name starting this way is
@code{auto-fill-mode}, so that is the completion.  You have now have
@samp{auto-fill-mode} in the minibuffer after typing just @kbd{au @key(TAB)
f @key(TAB)}.  Note that @key(TAB) has this effect because in the
minibuffer it is bound to the function @code{minibuffer-complete} when
completion is supposed to be done.

@kindex{SPC}
@cfindex{minibuffer-complete-word}
  @key(SPC) completes much like @key(TAB), but never adds goes beyond the
next hyphen.  If you have @samp{auto-f} in the minibuffer and type
@key(SPC), it finds that the completion is @samp{auto-fill-mode}, but it
stops completing after @samp{fill-}.  This gives @samp{auto-fill-}.
Another @key(SPC) at this point completes all the way to
@samp{auto-fill-mode}.  @key(SPC) in the minibuffer runs the function
@code{minibuffer-complete-word} when completion is available.

  There are three different ways that @key(RET) can work in completing
minibuffers, depending on how the argument will be used.

@itemize @bullet
@item
@dfn{Strict} completion is used when it is meaningless to give any
argument except one of the known alternatives.  For example, when
@kbd{C-x k} reads the name of a buffer to kill, it is meaningless to
give anything but the name of an existing buffer.  In strict
completion, @key(RET) refuses to exit if the text in the minibuffer
does not complete to an exact match.

@item
@dfn{Cautious} completion is similar to strict completion, except
that @key(RET) exits only if the text was an exact match already,
not needing completion.  If the text is not an exact match, @key(RET)
does not exit, but it does complete the text.  If it completes to an exact
match, a second @key(RET) will exit.

Cautious completion is used for reading file names for files that
must already exist.

@item
@dfn{Permissive} completion is used when any string whatever is
meaningful, and the list of completion alternatives is just a guide.
For example, when @kbd{C-x C-f} reads the name of a file to visit, any
file name is allowed, in case you want to create a file.  In permissive
completion, @key(RET) takes the text in the minibuffer exactly as
given, without completing it.
@end itemize

@vindex{completion-ignored-extensions}
  When completion is done on file names, certain file names are usually
ignored.  The variable @code{completion-ignored-extensions} contains a
list of strings; a file whose name ends in any of those strings is ignored
as a possible completion.  The standard value of this variable is
@code{(".o" ".elc" "~")}, which is designed to allow @samp{foo} to
complete to @samp{foo.c} even though @samp{foo.o} exists as well.
If the only possible completions are files that end in ``ignored''
strings, then they are not ignored.

@node[Repetition, , Completion, Minibuffer]

@section{Repeating Minibuffer Commands}
@setref Repetition

  Every command that uses the minibuffer at least once is recorded on a
special history list, together with the values of the minibuffer arguments,
so that you can repeat the command easily.  In particular, every
use of @kbd{Meta-x} is recorded, since @kbd{M-x} uses the minibuffer to
read the command name.

@c widecommands
@table 7
@item C-x @key(ESC)
Re-execute a recent minibuffer command.
@end table

@kindex{C-x ESC}
@cfindex{repeat-complex-command}
  @kbd{C-x @key(ESC)} (@code{repeat-complex-command}) is used to
re-execute a recent minibuffer-using command.  With no argument, it repeats
the last such command.  A numeric argument specifies which command to
repeat; one means the last one, and larger numbers specify earlier ones.

  @kbd{C-x @key(ESC)} works by turning the previous command into a Lisp
expression and then entering a minibuffer initialized with the text for that
expression.  If you type just @key(RET), the command is repeated as before.
You can also change the command by editing the Lisp expression.  Whatever
expression you finally submit is what will be executed.  The repeated command
does not go on the command history itself; @kbd{C-x @key(ESC)} does not
alter the command history.

@vindex{command-history}
  The list of previous minibuffer-using commands is stored as a Lisp list
in the variable @code{command-history}.  Each command is stored as a list
whose first element is the function called by the command and whose
remaining elements are the arguments that were given to it (values, not
expressions).  If @var[cmd] is an element of @code{command-history},
to repeat the command do @code{(apply (car @var[cmd]) (cdr @var[cmd]))}.
@c ??? Format should perhaps be changed

@node[M-x, Help, Minibuffer, Top]

@chapter[Running Commands by Name]
@setref M-x

  The Emacs commands that are used often or that must be quick to type are
bound to keys---short sequences of characters---for convenient use.  Other
Emacs commands that do not need to be brief are not bound to keys; to run
them, you must refer to them by name.

  A command name is, by convention, made up of one or words, separated by
hyphens; for example, @code{auto-fill-mode} or @code{manual-entry}.  The
use of English words makes the command name easier to remember than a key
made up of obscure characters, even though it is more characters to type.
Any command can be run by name, even if it is also runnable by keys.

@kindex{M-x}
@cfindex{execute-extended-command}
@cindex{minibuffer}
  The way to run a command by name is to start with @kbd{M-x}, type the
command name, and finish it with @key(RET).  Actually, @kbd{M-x} (the
command @code{execute-extended-command}) is using the minibuffer to read
the command name.

  Emacs uses the minibuffer for reading input for many different purposes;
on this occasion, the string @samp{M-x} is displayed at the beginning of
the minibuffer as a @dfn{prompt} to remind you that your input should be
the name of a command to be run.  @xref[Minibuffer], for full information
the features of the minibuffer.

  You can use completion to enter the command name.  For example, the
command @code{forward-char} can be invoked as an extended command by typing
@example
M-x forward-char @key(RET)
@exdent 1 or
M-x fo @key(TAB) c @key(RET)
@end example
@nopara
Note that @code{forward-char} is the same command that you invoke with
the key @kbd{C-f}.  Any command (interactively callable function) defined
in Emacs can be called by its name using @kbd{M-x} whether or not any
keys are bound to it.

  If you type @kbd{C-g} while the command name is being read, you cancel
the @kbd{M-x} command and get out of the minibuffer, ending up at top level.

  To pass a numeric argument to the command you are invoking with
@kbd{M-x}, specify the numeric argument before the @kbd{M-x}.  @kbd{M-x}
passes the argument along to the function which it calls.  The argument
value appears in the prompt while the command name is being read.

  Normally, when describing a command that is run by name, we omit the
@key(RET) that is needed to terminate the name.  Thus we might speak of
@kbd{M-x auto-fill-mode} rather than @kbd{M-x auto-fill-mode @key(RET)}.
We mention the @key(RET) only when there is a need to emphasize its
presence, such as when describing a sequence of input that contains a
command name and arguments that follow it.

@iftex
  In this manual, the convention for font usage is that Lisp objects,
including command names (which are Lisp symbols), appear in @code{this
font}, but keyboard input appears in @kbd{this font}.  This brings up
a problem with names of commands that are normally run by name: is the
name a piece of Lisp code, or is it a sequence of characters to type?
Unfortunately, it is both, but only one of the two fonts can be used.
I have chosen to use the Lisp object font when discussing the command,
as in @code{auto-fill-mode}, but to use the keyboard input font for
sequences of input, as in @kbd{M-x auto-fill-mode}.
@end iftex

@node[Help, Mark, M-x, Top]

@chapter[Help]
@setref Help
@kindex{Help}
@cindex{help}
@cindex{self-documentation}

  Emacs provides extensive help features which revolve around
a single character, @kbd{C-h}.  @kbd{C-h} is a prefix key that is used
only for documentation-printing commands.  The characters that you can type
after @kbd{C-h} are called @dfn{help options}.  One help option is
@kbd{C-h}; that is how you ask for help about using @kbd{C-h}.

  @kbd{C-h C-h} prints a list of the possible help options, and then asks
you to go ahead and type the option.  It prompts with a string
@example
A, C, F, I, K, L, M, N, T, V, W, C-c, C-d or C-h for more help: 
@end example
@nopara
and you should type one of those characters.  Typing a third @kbd{C-h}
displays a description of what the options mean; it still waits for
you to type an option.  To cancel, type @kbd{C-g}.

  Here is a summary of the defined help commands.

@table 7
@item C-h a
Display list of commands whose names contain a specified string.
@item C-h b
Display a table of all key bindings in effect now.
@item C-h c @var[key]
Print the name of the command that @var[key] runs.
@item C-h f @var[function] @key(RET)
Display documentation on the Lisp function named @var[function].
Note that commands are Lisp functions, so a command name may be used.
@item C-h k @var[key]
Display name and documentation of the command @var[key] runs.
@item C-h i
Run Info, the program for browsing documentation files.
@item C-h l
Display a description of the last 100 characters you typed.
@item C-h m
Display documentation of the current major mode.
@item C-h n
Display documentation of Emacs changes, most recent first.
@item C-h s
Display current contents of the syntax table, plus an explanation
of what they mean.
@item C-h t
Display the Emacs tutorial
@item C-h v @var[var] @key(RET)
Display the documentation of the Lisp variable @var[var].
@item C-h w @var[command] @key(RET)
Print which keys run the command named @var[command].
@end table

@kindex{C-h c}
@cfindex{describe-key-briefly}
  The most basic @kbd{C-h} options are @kbd{C-h c} and @kbd{C-h k}.
@kbd{C-h c @var[key]} prints in the echo area the name of
the command that @var[key] is bound to.  For example, @kbd{C-h c C-f}
prints @samp{forward-char}.  Since command names are chosen to describe
what the command does, this is a good way to get a very brief description
of what @var[key] does.  @kbd{C-h c} runs the command @code{describe-key-briefly}.

@kindex{C-h k}
@cfindex{describe-key}
  @kbd{C-h k @var[key]} is similar but gives more information.  It displays
the documentation string of the command @var[key] is bound to as well as
its name.  This is too big for the echo area, so a window is used for the
display.

@kindex{C-h f}
@cfindex{describe-function}
  @kbd{C-h f} (@code{describe-function}) reads the name of a Lisp function
using the minibuffer, then displays that function's documentation string
in a window.  Since commands are Lisp functions, you can use this to get
the documentation of a command that is known by name.  For example,
@example
C-h f auto-fill-mode @key(RET)
@end example
@nopara
displays the documentation of @code{auto-fill-mode}.  This is the only
way to see the documentation of a command that is not bound to any key
(one which you would normally call using @kbd{M-x}).

  @kbd{C-h f} is also useful for Lisp functions that you are planning to
use in a Lisp program.  For example, if you have just written the code
@code{(make-vector len)} and want to be sure that you are using
@code{make-vector} properly, type @kbd{C-h f make-vector @key(RET)}.
Because @kbd{C-h f} allows all function names, not just command names,
you may find that some of your favorite abbreviations that work in @kbd{M-x}
don't work in @kbd{C-h f}.  An abbreviation may be unique among command names
yet fail to be unique when other function names are allowed.

  The function name for @kbd{C-h f} to describe has a default which is
used if you type @key(RET) leaving the minibuffer empty.  The default is
the function called by the innermost Lisp expression in the buffer around
point, @i{provided} that is a valid, defined Lisp function name.  For
example, if point is located following the text @samp{(make-vector (car
x)}, the innermost list containing point is the one that starts with
@samp{(make-vector}, so the default is to describe the function
@code{make-vector}.

  @kbd{C-h f} is often useful just to verify that you have the right
spelling for the function name.  If @kbd{C-h f} mentions a default in the
prompt, you have typed the name of a defined Lisp function.  If that tells
you what you want to know, just type @kbd{C-g} to cancel the @kbd{C-h f}
command and go on editing.

@kindex{C-h v}
@cfindex{describe-variable}
  @kbd{C-h v} (@code{describe-variable}) is like @kbd{C-h f} but describes
Lisp variables instead of Lisp functions.  Its default is the Lisp symbol
around or before point, but only if that is the name of a known Lisp
variable.  @xref[Variables].

@kindex{C-h a}
@cfindex{command-apropos}
@cindex{apropos}
  A more complicated sort of question to ask is, ``What are the commands
for working with files?''  For this, type @kbd{C-h a file @key(RET)},
which displays a list of all command names that contain @samp{file}, such as
@code{copy-file}, @code{find-file}, and so on.  With each command name
appears a brief description of how to use the command, and what keys
you can currently invoke it with.  For example, it would say that
you can invoke @code{find-file} by typing @kbd{C-x C-f}.  The @kbd{a} in
@kbd{C-h a} stands for `Apropos'; @kbd{C-h a} runs the Lisp function
@code{command-apropos}.

  Because @kbd{C-h a} looks only for functions whose names contain the
string which you specify, you must use ingenuity in choosing substrings.
If you are looking for commands for killing backwards and @kbd{C-h a
kill-backwards @key(RET)} doesn't reveal any, don't give up.  Try just
@kbd{kill}, or just @kbd{backwards}, or just @kbd{back}.  Be persistent.
Pretend you are playing Adventure. 

  Here is a set of arguments to give to @code{apropos} that covers many
classes of Emacs commands, since there are strong conventions for naming
the standard Emacs commands.  By giving you a feel for the naming
conventions, this set should also serve to aid you in developing a
technique for picking @code{apropos} strings.
@itemize
char, line, word, sentence, paragraph, region, page, sexp, list, defun,
buffer, screen, window, file, dir, register, mode,
beginning, end, forward, backward, next, previous, up, down, search, goto,
kill, delete, mark, insert, yank, fill, indent, case,
change, set, what, list, find, view, describe.
@end itemize

@kindex{C-h w}
@cfindex{where-is}
  When @code{apropos} tells you of a command you might want to use, your next
question is probably whether any keys are bound to it.  @kbd{C-h w}
(@code{where-is}) is the command to tell you this.  It reads a command name
as a minibuffer argument and prints in the echo area some keys that are bound
to that command.  Alternatively, it says that the command is not on any keys,
which implies that you must use @kbd{M-x} to call it.

@kindex{C-h l}
@cfindex{view-lossage}
  If something surprising happens, and you are not sure what commands you
typed, use @kbd{C-h l} (@code{view-lossage}).  @kbd{C-h l} prints the last 100
command characters you typed in.  If you see commands that you don't know,
you can use @kbd{C-h c} to find out what they do.

@kindex{C-h m}
@cfindex{describe-mode}
  Emacs has several major modes, each of which redefines a few keys and
makes a few other changes in how editing works.  @kbd{C-h m}
(@code{describe-mode}) prints documentation on the current major mode,
which normally describes all the commands that are changed in this mode.

@kindex{C-h i}
@cfindex{info}
@kindex{C-h n}
@cfindex{view-emacs-news}
@kindex{C-h t}
@cfindex{help-with-tutorial}
@kindex{C-h C-c}
@cfindex{describe-copying}
@kindex{C-h C-d}
@cfindex{describe-distribution}
  The other @kbd{C-h} options display various files of useful information.
@kbd{C-h n} (@code{view-emacs-news}) displays the file
@code{emacs/etc/NEWS}, which contains documentation on Emacs changes
arranged chronologically.  @kbd{C-h t} (@code{help-with-tutorial}) displays
the learn-by-doing Emacs tutorial.  @kbd{C-h i} (@code{info}) runs the Info
program, which is used for browsing through structured documentation files.
@kbd{C-h C-c} (@code{describe-copying}) displays the file
@code{emacs/etc/COPYING}, which tells you the conditions you should obey in
distributing copies of Emacs.  @kbd{C-h C-d} (@code{describe-distribution})
displays the file @code{emacs/etc/DISTRIB}, which tells you how you can
order a copy of the latest version of Emacs.

@node[Mark, Killing, Help, Top]

@chapter[The Mark and the Region]
@setref Mark
@cindex{mark}@cindex{region}

  There are many Emacs commands which operate on an arbitrary contiguous
part of the current buffer.  To specify the text for such a command to
operate on, you set @dfn{the mark} at one end of it, and move point to the
other end.  The text between point and the mark is called @dfn{the region}.
You can move point or the mark to adjust the boundaries of the region.  It
doesn't matter which one is set first chronologically, or which one comes
earlier in the text.

  Once the mark has been set, it remains until it is set again at another
place.  The mark remains fixed with respect to the preceding character if
text is inserted or deleted in the buffer.  Each Emacs buffer has its own
mark, so that when you return to a buffer that had been selected
previously, it has the same mark it had before.

  Many commands that insert text, such as @kbd{C-y} (@code{yank}) and
@kbd{M-x Insert Buffer}, position the mark at one end of the inserted
text---the opposite end from where point is positioned, so that the region
contains the text just inserted.

  Here are some commands for setting the mark:

@c WideCommands
@table 7
@item C-@key(SPC)
Set the mark where point is.
@item C-@@
The same.
@item C-x C-x
Interchange mark and point.
@item M-@@
Set mark after end of next word.  This command and the following
three do not move point.
@item C-M-@@
Set mark after end of next Lisp expression.
@item C-<
Set mark at beginning of buffer.
@item C->
Set mark at end of buffer.
@item M-h
Put region around current paragraph.
@item C-M-h
Put region around current Lisp defun.
@item C-x h
Put region around entire buffer.
@item C-x C-p
Put region around current page.
@end table

  For example, if you wish to convert part of the buffer to all upper-case,
you can use the @kbd{C-x u} (@code{upcase-region}) command, which operates on the text in the
region.  You can first go to the beginning of the text to be capitalized,
type @kbd{C-@key(SPC)} to put the mark there, move to the end, and then
type @kbd{C-x C-u}.  Or, you can set the mark at the end of the text, move
to the beginning, and then type @kbd{C-x C-u}.  Most commands that operate
on the text in the region have the word @code{region} in their names.

@kindex{C-SPC}@cfindex{set-mark-command}
  The most common way to set the mark is with the @kbd{C-@key(SPC)}
command (@code{set-mark-command}).  This sets the mark where point is.
Then you can move point away, leaving the mark behind.  It is actually
incorrect to speak of the character @kbd{C-@key(SPC)}; there is no
such character.  When you type @key(SPC) while holding down control,
what you get on most terminals is the character @kbd{C-@@}.  This is
the key actually bound to @code{set-mark-command}.  But unless you
are unlucky enough to have a terminal where typing @kbd{C-@key(SPC)}
does not produce @kbd{C-@@}, you might as well think of this character
as @kbd{C-@key(SPC)}.

@kindex{C-x C-x}@cfindex{exchange-dot-and-mark}
  Since terminals have only one cursor, there is no way for Emacs to show
you where the mark is located.  You have to remember.  The usual solution
to this problem is to set the mark and then use it soon, before you forget
where it is.  But you can see where the mark is with the command @kbd{C-x
C-x} (@code{exchange-dot-and-mark}) which puts the mark where point was and
point where the mark was.  The extent of the region is unchanged, but the
cursor and point are now at the previous location of the mark.

  @kbd{C-x C-x} is also useful when you are satisfied with the location of
point but want to move the mark; do @kbd{C-x C-x} to put point there and
then you can move it.  A second use of @kbd{C-x C-x}, if necessary, puts
the mark at the new location with point back at its original location.

@section[Operating on the Region]

  Once you have created an active region, you can do many things to
the text in it:
@itemize @bullet
@item
Kill it with @kbd{C-w} (@pxref[Killing]).
@item
Save it in a register with @kbd{C-x x} (@pxref[Registers]).
@item
Save it in a buffer or a file (@pxref[Accumulating Text]).
@item
Convert case with @kbd{C-x C-l} or @kbd{C-x C-u} (@pxref[Case]).
@item
Evaluate it as Lisp code with @kbd{M-x eval-region} (@pxref[Lisp Eval]).
@item
Fill it as text with @kbd{M-g} (@pxref[Filling]).
@item
Print hardcopy with @kbd{M-x print-region} (@pxref[Hardcopy]).
@item
Indent it with @kbd{C-x @key(TAB)} or @kbd{C-M-\} (@pxref[Indentation]).
@end itemize

@section[Commands to Mark Textual Objects]

@kindex{M-@@}
@kindex{C-M-@@}
@cfindex{mark-word}
@cfindex{mark-sexp}
  There are commands for placing the mark on the other side of a certain
object such as a word or a list, without having to move there first.
@kbd{M-@@} (@code{mark-word}) puts the mark at the end of the next word,
while @kbd{C-M-@@} (@code{mark-sexp}) puts it at the end of the next Lisp
expression.  These characters allow you to save a little typing or
redisplay, sometimes.

@kindex{M-h}
@kindex{C-M-h}
@kindex{C-x C-p}
@kindex{C-x h}
@cfindex{mark-paragraph}
@cfindex{mark-defun}
@cfindex{mark-page}
@cfindex{mark-whole-buffer}
   Other commands set both point and mark, to delimit an object in the
buffer.  @kbd{M-h} (@code{mark-paragraph}) moves point to the beginning of
the paragraph that surrounds or follows point, and puts the mark at the end
of that paragraph (@pxref[Paragraphs]).  @kbd{M-h} does all that's
necessary if you wish to indent, case-convert, or kill a whole paragraph.
@kbd{C-M-h} (@code{mark-defun}) similarly puts point before and the mark
after the current or following defun (@pxref[Defuns]).  @kbd{C-x C-p}
(@code{mark-page}) puts point before the current page (or the next or
previous, according to the argument), and mark at the end (@pxref[Pages]).
The mark goes after the terminating page delimiter (to include it), while
point goes after the preceding page delimiter (to exclude it).  Finally,
@kbd{C-x h} (@code{mark-whole}) sets up the entire buffer as the region, by
putting point at the beginning and the mark at the end.

@section[The Mark Ring]
@setref Mark Ring

@kindex{C-u C-SPC}
@cindex{mark ring}
@kindex{C-u C-@@}
  Aside from delimiting the region, the mark is also useful for remembering
a spot that you may want to go back to.  To make this feature more useful,
Emacs remembers 16 previous locations of the mark, in the @code{mark
ring}.  Most commands that set the mark push the old mark onto this ring.
To return to a marked location, use @kbd{C-u C-@@} (or @kbd{C-u C-@key(SPC)});
this is the command @code{set-mark-command} given a numeric argument.
This moves point to where the mark was, and restores the mark from the ring
of former marks.  So repeated use of this command moves point to all of the
old marks on the ring, one by one.  Enough uses of @kbd{C-u C-@@} bring
point back to where it was originally.

  Each buffer has its own mark ring.  All editing commands that use
the mark ring use the current buffer's mark ring.  In particular,
@kbd{C-u C-@key(SPC)} always stays in the same buffer.

  Many commands that can move long distances, such as @kbd{M-<}
(@code{beginning-of-buffer}) and @kbd{C-M-a} (@code{beginning-of-defun}),
start by setting the mark and saving the old mark on the mark ring, just as
a way of making it possible for you to move to where point was before the
command.  This is to make it easier for you to move back later.  Searches
record the starting point except when they do not actually move.  You can
tell when a command sets the mark because @samp{Mark Set} is printed in the
echo area.

@vindex{mark-ring-max}
  The variable @code{mark-ring-max} is the maximum number of entries to
keep in the mark ring.  If that many entries exist and another one is
pushed, the last one in the list is discarded.  Repeating
@kbd{C-u C-@key(SPC)} circulates through the limited number of
entries that are currently in the ring.

@vindex{mark-ring}
  The variable @code{mark-ring} holds the mark ring itself, as a list of
marker objects in the order most recent first.

@iftex
@chapter(Killing and Moving Text)

  @dfn{Killing} means erasing text and copying it into the @dfn{kill
ring}, from which it can be retrieved by @dfn{yanking} it.

  The commonest way of moving or copying text with Emacs is to kill it and
later yank it in one or more places.  This is very safe because all the
text ever killed is remembered, and it is versatile, because the many
commands for killing syntactic units can also be used for moving those
units.  There are also other ways of copying text for special purposes.

  Emacs has only one kill ring, so you can kill text in one buffer and
yank it in another buffer.

@end iftex

@node[Killing, Yanking, Mark, Top]

@section(Deletion and Killing)
@setref Killing
@cfindex{delete-char}
@c ??? Should be backward-delete-char
@cfindex{delete-backward-char}

@cindex{killing}@cindex{deletion}
@kindex{C-d}
@kindex{DEL}
  Most commands which erase text from the buffer save it so that you can
get it back if you change your mind, or move or copy it to other parts of
the buffer.  These commands are known as @dfn[kill] commands.  The rest of
the commands that erase text do not save it; they are known as @dfn[delete]
commands.  The delete commands include @kbd{C-d} (@code{delete-char}) and
@key(DEL) (@code{delete-backward-char}), which 
delete only one character at a time, and those commands that delete only
spaces or newlines.  Commands that can destroy significant amounts of
nontrivial data generally kill.  The commands' names and individual
descriptions use the words @samp{kill} and @samp{delete} to say which they
do.  If you do a kill or delete command by mistake, you can use the
@kbd(C-x u) (@code{undo}) command to undo it (@pxref[Undo]).

@subsection[Deletion]

@table 7
@item C-d
Delete next character.
@item @key(DEL)
Delete previous character.
@item M-\
Delete spaces and tabs around point.
@item M-@key(SPC)
Delete spaces and tabs around point, leaving one space.
@item C-x C-o
Delete blank lines around the current line.
@item M-^
Join two lines by deleting the intervening newline, and any indentation
following it.
@end table

  The most basic delete commands are @kbd{C-d} (@code{delete-char})
and @key(DEL) (@code{delete-backward-char}).  @kbd{C-d} deletes the
character after point, the one the cursor is ``on top of''.  Point
doesn't move.  @key(DEL) deletes the character before the cursor,
and moves point back.  Newlines can be deleted like any other
characters in the buffer; deleting a newline joins two lines.
Actually, @kbd{C-d} and @key(DEL) aren't always delete commands; if
given an argument, they kill instead, since they can erase more than
one character this way.

@kindex{M-Backslash}
@cfindex{delete-horizontal-space}
@kindex{M-SPC}
@cfindex{just-one-space}
@kindex{C-x C-o}
@cfindex{delete-blank-lines}
@kindex{M-^}
@cfindex{delete-indentation}
  The other delete commands are those which delete only formatting
characters: spaces, tabs and newlines.  @kbd{M-\}
(@code{delete-horizontal-space}) deletes all the spaces and tab
characters before and after point.  @kbd{M-@key(SPC)} (@code{just-one-space})
does likewise but leaves a single space after point, regardless of the
number of spaces that existed previously (even zero).

  @kbd{C-x C-o} (@code{delete-blank-lines}) deletes all blank lines
after the current line, and if the current line is blank deletes all
blank lines preceding the current line as well (leaving one blank
line, the current line).  @kbd{M-^} (@code{delete-indentation}) joins
the current line and the previous line, or the current line and the
next line if given an argument, by deleting a newline and all
surrounding spaces, possibly leaving a single space.
@xref[Indentation,M-^].

@subsection[Killing by Lines]

@table 7
@item C-k
Kill rest of line or one or more lines.
@end table

@kindex{C-k}
@cfindex{kill-line}
  The simplest kill command is @kbd{C-k} (@code{kill-line}).  If given at
the beginning of a line, it kills all the text on the line, leaving it
blank.  If given on a blank line, the blank line disappears.  As a
consequence, if you go to the front of a non-blank line and type
@kbd{C-k} twice, the line disappears completely.

  More generally, @kbd{C-k} kills from point up to the end of the line,
unless it is at the end of a line.  In that case it kills the newline
following the line, thus merging the next line into the current one.
Invisible spaces and tabs at the end of the line are ignored when deciding
which case applies, so if point appears to be at the end of the line, you
can be sure the newline will be killed.

  If @kbd{C-k} is given a positive argument, it kills that many lines and
the newlines that follow them (however, text on the current line before
point is spared).  With a negative argument, it kills back to a number of
line beginnings.  An argument of -2 means kill back to the second line
beginning.  If point is at the beginning of a line, that line beginning
doesn't count, so @kbd[C-u - 2 C-k] with point at the front of a line kills
the two previous lines.

  @kbd{C-k} with an argument of zero kills all the text before point on the
current line.

@subsection[Other Kill Commands]
@cfindex{kill-line}
@cfindex{kill-region}
@cfindex{kill-word}
@cfindex{backward-kill-word}
@cfindex{kill-sexp}
@cfindex{backward-kill-xsexp}
@cfindex{kill-sentence}
@cfindex{backward-kill-sentence}
@kindex{M-d}
@kindex{M-DEL}
@kindex{C-M-k}
@kindex{C-M-DEL}
@kindex{C-x DEL}
@kindex{M-k}
@kindex{C-k}
@kindex{C-w}

@c DoubleWideCommands
@table 7
@item C-w
Kill region (from point to the mark).
@item M-d
Kill word.
@item M-@key(DEL)
Kill word backwards.
@item C-x @key(DEL)
Kill back to beginning of sentence (@pxref[Sentences]).
@item M-k
Kill to end of sentence.
@item C-M-k
Kill sexp (@pxref[Lists]).
@item C-M-@key(DEL)
Kill sexp backwards.
@item M-z @var[char]
Kill up to next occurrence of @var[char].
@end table

  A kill command which is very general is @kbd{C-w} (@code{kill-region}),
which kills everything between point and the mark.  With this command, you
can kill any contiguous sequence of characters, if you first set the mark
at one end of them and go to the other end.

@kindex{M-z}
@cfindex{zap-to-char}
  A convenient way of killing is combined with searching: @kbd{M-z}
(@code{zap-to-char}) reads a character and kills from point up to (but not
including) the next occurrence of that character in the buffer.  If there
is no next occurrence, killing goes to the end of the buffer.  A numeric
argument acts as a repeat count.  A negative argument means to search
backward and kill text before point.

  Other syntactic units can be killed: words, with
@kbd{M-@key(DEL)} and @kbd{M-d} (@pxref[Words]); sexps,
with @kbd{C-M-k} (@pxref[Lists]); and sentences, with @kbd{C-x
@key(DEL)} and @kbd{M-k} (@pxref[Sentences]).@refill

@node[Yanking, Accumulating Text, Killing, Top]

@section[Yanking]
@setref Yanking
@cindex{moving text}
@cindex{kill ring}
@cindex{yanking}

  @dfn{Yanking} is getting back text which was killed.  The usual way to
move or copy text is to kill it and then yank it one or more times.

@table 7
@item C-y
Yank last killed text.
@item M-y
Replace re-inserted killed text with the previously killed text.
@item M-w
Save region as last killed text without actually killing it.
@item C-M-w
Append next kill to last batch of killed text.
@end table

@kindex{C-y}
@cfindex{Yank}
  All killed text is recorded in the @dfn{kill ring}, a list of blocks
of text that have been killed.  There is only one kill ring, used in
all buffers, so you can kill text in one buffer and yank it in another
buffer.

  The command @kbd{C-Y} (@code[Yank]) reinserts the text of the most recent
kill.  It leaves the cursor at the end of the text.  It sets the mark at
the beginning of the text.  @xref[Mark].

  @kbd{C-u C-y} leaves the cursor in front of the text, and sets the
mark after it.  This is only if the argument is specified with just a
@kbd{C-u}, precisely.  Any other sort of argument, including @kbd{C-u}
and digits, has an effect described below (under ``Yanking Earlier
Kills'').

@kindex{M-w}
@cfindex{copy-region-as-kill}
  If you wish to copy a block of text, you might want to use @kbd{M-W}
(@code{copy-region-as-kill}), which copies the region into the kill
ring without removing it from the buffer.  This is approximately
equivalent to @kbd{C-w} followed by @kbd{C-y}, except that @kbd{M-w}
does not mark the buffer as ``modified'' and does not temporarily
change the screen.

@subsection[Appending Kills]

  Normally, each kill command pushes a new block onto the kill ring.
However, two or more kill commands in a row combine their text into a
single entry, so that a single @kbd{C-y} gets it all back as it was
before it was killed.  This means that you don't have to kill all the text
in one command; you can keep killing line after line, or word after word,
until you have killed it all, and you can still get it all back at once.
(Thus we join television in leading people to kill thoughtlessly.)

  Commands that kill forward from point add onto the end of the previous
killed text.  Commands that kill backward from point add onto the
beginning.  This way, any sequence of mixed forward and backward kill
commands puts all the killed text into one entry without rearrangement.
Numeric arguments do not break the sequence of appending kills.  For
example, suppose the buffer contains
@example
This is the first
line of sample text
and here is the third.
@end example
@nopara
with point at the beginning of the second line.  If you type
@kbd{C-k C-u 2 M-@key(DEL) C-k}, the first @kbd{C-k} kills the text
@samp{line of sample text}, @kbd{C-u 2 M-@key(DEL)} kills @samp{the first}
with the newline that followed it, and the second @kbd{C-k} kills the
newline after the second line.  The result is that the buffer contains
@samp{This is and here is the third.} and a single kill entry contains
@samp{the first@key(RET)line of sample text@key(RET)}---all the
killed text, in its original order.

@kindex{C-M-w}@cfindex{append-next-kill}
  If a kill command is separated from the last kill command by other
commands (not just numeric arguments), it starts a new entry on the kill
ring.  But you can force it to append by first typing the command
@kbd{C-M-w} (@code{append-next-kill}) in front of it.  The @kbd{C-M-w}
tells the following command, if it is a kill command, to append the text it
kills to the last killed text, instead of starting a new entry.  With
@kbd{C-M-w}, you can kill several separated pieces of text and accumulate
them to be yanked back in one place.

@subsection[Yanking Earlier Kills]

@kindex{M-y}@cfindex{yank-pop}
  To recover killed text that is no longer the most recent kill, you need
the @kbd{Meta-y} (@code{yank-pop}) command.  @kbd{M-y} should
be used only after a @kbd{C-y} or another @kbd{M-y}.  It takes the
text previously yanked and replaces it with the text from an earlier kill.
So, to recover the text of the next-to-the-last kill, you first use
@kbd{C-y} to recover the last kill, and then use @kbd{M-y} to replace it
with the previous kill.

  You can think in terms of a ``last yank'' pointer which points at an item
in the kill ring.  Each time you kill, the ``last yank'' pointer moves
to the newly made item at the front of the ring.  @kbd{C-y} yanks the
item which the ``last yank'' pointer points to.  @kbd{M-y} moves the ``last
yank'' pointer to a different item, and the text in the buffer changes to
match.  Enough @kbd{M-y} commands can move the pointer to any item in the
ring, so you can get any item into the buffer.  Eventually the pointer
reaches the end of the ring; the next @kbd{M-y} moves it to the first
item again.

  @kbd{M-y} can take a numeric argument, which tells it how many items to
advance the ``last yank'' pointer by.  A negative argument moves the
pointer toward the front of the ring; from the front of the ring,
it moves to the last entry and starts moving forward from there.

  Once the text you are looking for is brought into the buffer, you
can stop doing @kbd{M-y} commands and it will stay there.  It's just a
copy of the kill ring item, so editing it in the buffer does not
change what's in the ring.  As long as no new killing is done, the
``last yank'' pointer remains at the same place in the kill ring, so
repeating @kbd{C-y} will yank another copy of the same old kill.

  If you know how many @kbd{M-y} commands it would take to find the
text you want, you can yank that text in one step using @kbd{C-y} with
a numeric argument.  @kbd{C-y} with an argument greater than one
restores the text the specified number of entries back in the kill
ring.  Thus, @kbd[C-u 2 C-y] gets the next to the last block of killed
text.  It is equivalent to @kbd{C-y M-y}.  @kbd{C-y} with a numeric
argument starts counting from the ``last yank'' pointer, and sets the
``last yank'' pointer to the entry that it yanks.

@vindex{kill-ring-max}
  The length of the kill ring is controlled by the variable
@code{kill-ring-max}; no more than that many blocks of killed text are
saved.

@node[Accumulating Text, Registers, Yanking, Top]

@section[Accumulating Text]
@setref Accumulating Text
@kindex{C-x a}
@cfindex{append-to-buffer}
@cfindex{prepend-to-buffer}
@cfindex{copy-to-buffer}
@cfindex{append-to-file}

  Usually we copy or move text by killing it and yanking it, but
there are other ways that are useful for copying one block of text in
many places, or for copying many scattered blocks of text into one
place.

  You can accumulate blocks of text from scattered locations either
into a buffer or into a file if you like.  These commands are
described here.  You can also use Emacs registers for storing and
accumulating text.  @xref[Registers].

@table 7
@item C-x a
Append region to contents of specified buffer.
@item M-x prepend-to-buffer
Prepend region to contents of specified buffer.
@item M-x copy-to-buffer
Copy region into specified buffer.
@item M-x insert-buffer
Insert contents of specified buffer into current buffer at point.
@item M-x append-to-file
Append region to contents of specified file, at the end.
@end table

  To accumulate text into a buffer, use the command @kbd{C-x a
@var[buffername]} (@code{append-to-buffer}), which inserts a copy of the
region into the buffer @var{buffername}, at the location of point in that
buffer.  If there is no buffer with that name, one is created.  If you
append text into a buffer which has been used for editing, the copied text
goes into the middle of the text of the buffer, wherever point happens to
be in it.

  Point in that buffer is left at the end of the copied text, so successive
uses of @kbd{C-x a} accumulate the text in the specified buffer in the same
order as they were copied.  Strictly speaking, @kbd{C-x a} does not always
append to the text already in the buffer; but if @kbd{C-x a} is the only
command used to alter a buffer, it does always append to the existing text
because point is always at the end.

  @kbd{M-x prepend-to-buffer} is just like @kbd{C-x a} except
that point in the other buffer is left before the copied text, so
successive prependings add text in reverse order.  @kbd{M-x copy-to-buffer}
is similar except that any existing text in the other buffer is deleted, so
the buffer is left containing just the text newly copied into it.

  You can retrieve the accumulated text from that buffer with @kbd{M-x
insert-buffer}; this too takes @var{buffername} as an argument.  It inserts
a copy of the text in buffer @var{buffername} into the selected buffer.
You could alternatively select the other buffer for editing, perhaps moving
text from it by killing or with @kbd{C-x a}.  @xref[Buffers], for
background information on buffers.

  Instead of accumulating text within Emacs, in a buffer, you can append
text directly into a file with @kbd{M-x append-to-file}, which takes
@var{file-name} as an argument.  It adds the text of the region to the end
of the specified file.  The file is changed immediately on disk. This
commands is normally used with files that are @i[not] being visited in
Emacs.  Using them on files that Emacs is visiting can produce confusing
results, because the text inside Emacs for those files will not change.

@section{Rectangles}
@setref Rectangles
@cindex{rectangles}

  The rectangle commands affect rectangular areas of the text: all the
characters between a certain pair of columns, in a certain range of lines.
Commands are provided to kill rectangles, yank killed rectangles, clear
them out, or delete them.

  When you must specify a rectangle for a command to work on, you do
it by putting the mark at one corner and point at the opposite corner.
The rectangle thus specified is called the @dfn{region-rectangle}
because it is controlled about the same way the region is controlled.
But remember that a given combination of point and mark values can be
interpreted either as specifying a region or as specifying a
rectangle; it is up to the command that uses them to choose the
interpretation.

@table 7
@item M-x delete-rectangle
Delete the text of the region-rectangle, moving any following text on
each line leftward to the left edge of the region-rectangle.
@item M-x kill-rectangle
Similar, but also save the contents of the region-rectangle as the
``last killed rectangle''.
@item M-x yank-rectangle
Yank the last killed rectangle with its upper left corner at point.
@item M-x open-rectangle
Insert blank space to fill the space of the region-rectangle.
The previous contents of the region-rectangle are pushed rightward.
@item M-x clear-rectangle
Clear the region rectangle by replacing its contents with spaces.
@end table

  The rectangle operations fall into two classes: commands deleting and
moving rectangles, and commands for blank rectangles.

@cfindex{delete-rectangle}
@cfindex{kill-rectangle}
  There are two ways to delete a rectangle: you can discard its contents,
or save them as the ``last killed'' rectangle.  The commands for these
three ways are @kbd{M-x delete-rectangle} and @kbd{M-x kill-rectangle}.  In
any case, the portion of each line that falls inside the rectangle's
boundaries is deletyed, causing following text (if any) on the line to move
left.

  Note that ``killing'' a rectangle is not killing in the usual sense; the
rectangle is not stored in the kill ring, but in a special place that
can only record the most recent rectangle killed.  This is because yanking
a rectangle is so different from yanking linear text that different yank
commands have to be used and yank-popping is hard to make sense of.

  Inserting a rectangle is the opposite of deleting one.  All you need to
specify is where to put the upper left corner; that is done by putting
point there.  The rectangle's first line is inserted there, the rectangle's
second line is inserted at a point one line vertically down, and so on.
The number of lines affected is determined by the height of the saved
rectangle.

@cfindex{yank-rectangle}
  To insert the last killed rectangle, type @kbd{M-x yank-rectangle}.

@cfindex{open-rectangle}
@cfindex{clear-rectangle}
  There are two commands for working with blank rectangles: @kbd{M-x
clear-rectangle} to blank out existing text, and @kbd{M-x open-rectangle}
to insert a blank rectangle.  Clearing a rectangle is equivalent to
deleting it and then inserting as blank rectangle of the same size.

  Rectangles can also be copied into and out of registers.  @xref[Rectangle
Registers].

@node[Registers,Undo,Killing,Top]
@chapter[Registers]
@setref Registers
@cindex{registers}

  Emacs @dfn{registers} are places you can save text or positions for
use later.  Text saved in a register can be copied into the buffer
once or many times; a position saved in a register is used by moving
point to that position.  Rectangles can also be copied into and out of
registers (@pxref[Rectangles]).

  Each register has a name, which is a single character.  It can store
either a piece of text or a position or a rectangle; only one of the three
at any given time.  Whatever you store in a register remains there until
you store something else in that register.

@section[Saving Positions in Registers]

@table 7
@item C-x / @var{r}
Save location of point in register @var{r}.
@item C-x j @var{r}
Jump to the location saved in register @var{r}.
@end table

@kindex{C-x /}
@cfindex{dot-to-register}
  To save the current location of point in a register, choose a name
@var{r} and type @kbd{C-x / @var[r]}.  (@kbd{C-x /} runs the command
@code{dot-to-register}.)  The register @var{r} retains the location thus
saved until you store something else in that register.

@kindex{C-x j}
@cfindex{register-to-dot}
  The command @kbd{C-x j @var[r]} (@code{register-to-dot}) moves
point to the location recorded in register @var{r}.  The register is not
affected; it continues to record the same location.  You can jump to the
same position using the same register any number of times.

@section[Saving Text in Registers]

  When you want to insert a copy of the same piece of text frequently, it
may be impractical to use the kill ring, since each subsequent kill moves
the piece of text farther down on the ring.  It becomes hard to keep track
of what argument is needed to retrieve the same text with @kbd{C-y}.  An
alternative is to store the text in a register with @kbd{C-x x}
(@code{copy-to-register}) and then retrieve it with @kbd{C-x g}
(@code{insert-register}).

@table 7
@item C-x x @var(r)
Copy region into register @var(r).
@item C-x g @var(r)
Insert text contents of register @var(r).
@end table

@kindex{C-x x}
@kindex{C-x g}
@cfindex{copy-to-register}
@cfindex{insert-register}
  @kbd{C-x x @var[r]} stores a copy of the text of the region into the
register named @var{r}.  Given a numeric argument, @kbd{C-x x} deletes the
text from the buffer as well.

  @kbd{C-x g @var[r]} inserts in the buffer the text from register @var{r}.
Normally it leaves point before the text and places the mark after, but
with a numeric argument it puts point after the text and the mark before.

@section{Saving Rectangles in Registers}
@setref Rectangle Registers
@cindex{rectangle}

  A register can contain a rectangle instead of linear text.  The rectangle
is represented as a list of strings.  @xref[Rectangles], for basic
information on rectangles and how rectagles in the buffer are specified.

@table 7
@item C-x r @var[r]
Copy the region-rectangle into register @var[r]
(@code{copy-region-to-rectangle}).  With numeric argument, delete it as well.
@item C-x g @var[r]
Insert the rectangle stored in register @var[r] (if it contains a
rectangle).
@end table

  The @kbd{C-x g} command inserts linear text if the register contains
that, or inserts a rectangle if the register contains one.

@section[Getting Information about Registers]

@table 7
@item M-x view-register @key(RET) @var[r]
Display a description of what register @var[r] contains.
@end table

@cfindex{view-register}
  @kbd{M-x view-register} reads a register name as an argument and then
displays the contents of the specified register.

@node[Undo,Display,Registers,Top]

@chapter[Undoing Changes]
@setref Undo
@cindex{undo}

  Emacs allows all changes made in the text of a buffer to be undone,
up to a certain amount of change (8000 characters).  Each buffer records
changes individually, and the undo command always applies to the
current buffer.  Usually each editing command makes a separate entry
in the undo records, but some commands such as @code{query-replace}
make many entries, and very simple commands such as self-inserting
characters are often grouped to make undoing less tedious.

@table 7
@item C-x u
Undo one batch of changes (usually, one command worth).
@item C-_
The same.
@end table

@kindex{C-x u}
@kindex{C-_}
@cfindex{undo}
  The command @kbd{C-x u} or @kbd{C-_} (@code{undo}) is how you undo.
The first time you give this command, it undoes the last change.
Point moves to the beginning of the text affected by the undo,
so you can see what was undone.

  Consecutive repetitions of the @kbd{C-_} or @kbd{C-x u} commands undo
earlier and earlier changes, back to the limit of what has been recorded.
If all recorded changes have already been undone, the undo command gets an
error.

  Any command other than an undo command breaks the sequence of undo
commands.  Starting at this moment, the previous undo commands are considered
ordinary changes that can themselves be undone.  Thus, you can redo changes
you have undone by typing @kbd{C-@key(SPC)}, @kbd{C-f} or any other
command that will have no important effect, and then using more undo commands.

  If you notice that a buffer has been modified accidentally, the easiest
way to recover is to type @kbd{C-_} repeatedly until the stars disappear
from the front of the mode line.  At this time, all the modifications you
made have been cancelled.  If you do not remember whether you changed the
buffer deliberately, type @kbd{C-_} once, and when you see the last change
you made undone, you will remember why you made it.  If it was an accident,
leave it undone.  If it was deliberate, redo the change as described in
the preceding paragraph.

  Not all buffers record undo information.  Buffers whose names start with
spaces don't; these buffers are used internally by Emacs and its extensions
to hold text that users don't normally look at or edit.  Also, minibuffers,
help buffers and documentation buffers don't record undo information.

  At most 8000 or so characters of deleted or modified text can be remembered
in any one buffer for reinsertion by the undo command.  Also, there is a limit
on the number of individual insert, delete or change actions that can be
remembered.

  The reason the @code{undo} command has two keys, @kbd{C-x u} and
@kbd{C-_}, set up to run it is that it is worthy of a single-character
key, but the way to type @kbd{C-_} on some keyboards is not obvious.
@kbd{C-x u} is an alternative that requires no special knowledge of
the terminal.

@node[Display,Search,Undo,Top]

@chapter[Controlling the Display]
@setref Display
@cindex{scrolling}

  Since only part of a large buffer fits in the window, Emacs tries to show
the part that is likely to be interesting.  The display control commands
allow you to ask to see a different part of the text.  This is also known
as @dfn{scrolling}.

  If a buffer contains text that is too large to fit entirely within a
window that is displaying the buffer, Emacs shows a contiguous section of
the text.  The section shown always contains point.  As you change the
text, Emacs always tries to keep the same position in the text at the top
of the window.  A new position moves to the top of the window only if this
is necessary to keep point visible, or if you request it explicitly with a
display control command.

@table 7
@item C-l
Clear screen and redisplay, scrolling the selected window to center point
vertically within it.
@item C-v
Scroll forwards (a windowful or a few lines).
@item M-v
Scroll backwards.
@item C-x <
Scroll display of lines to the left.
@item C-x >
Scroll display of lines to the right.
@item M-r
Move point to the text at a given vertical position within the window.
@end table

@kindex{C-l}@cfindex{recenter}
  The basic display control command is @kbd{C-l} (@code{recenter}).
In its simplest form, with no argument, it clears the entire screen
and redisplays all windows, scrolling the selected window so that
point is halfway down from the top of the window.  Other windows are
cleared and redisplayed, but not scrolled.

  @kbd{C-l} with a numeric argument does not clear the screen; it does
nothing except scroll the selected window as specified by the argument.
With a positive argument @var[n], it repositions text to put point @var[n]
lines down from the top.  An argument of zero puts point on the very top
line.  Point does not move with respect to the text; rather, the text and
point move rigidly on the screen.  @kbd{C-l} with a negative argument puts
point that many lines from the bottom of the window.  For example, @kbd{C-u
- 1 C-l} puts point on the bottom line, and @kbd{C-u - 5 C-l} puts it five
lines from the bottom.

@kindex{C-v}@kindex{M-v}
@cfindex{scroll-up}@cfindex{scroll-down}
  The @dfn[scrolling] commands @kbd{C-v} and @kbd{M-v} let you move the
whole display up or down a few lines.  @kbd{C-v} (@code{scroll-up}) with an
argument shows you that many more lines at the bottom of the window, moving
the text and point up together as @kbd{C-l} might.  @kbd{C-v} with a
negative argument shows you more lines at the top of the window.
@kbd{Meta-v} (@code{scroll-down}) is like @kbd{C-v}, but moves in the
opposite direction.

@vindex{next-screen-context-lines}
  To read the buffer a windowful at a time, use @kbd{C-v} with
no argument.  It takes the last line at the bottom of the window and
puts it at the top, followed by nearly a whole windowful of lines not
visible before.  Point is put at the top of the window.  Thus, each
@kbd{C-v} shows the ``next windowful'', except for one line of overlap to
provide continuity.  @kbd{M-v} with no argument moves the same distance
backward.  The number of lines of overlap across a @kbd{C-v} is controlled
by the variable @code{next-screen-context-lines}; by default, it is two.

@vindex{scroll-step}
  Scrolling happens automatically if point has moved out of the visible
portion of the text when it is time to display.  Usually the scrolling is
done so as to put point vertically centered within the window.  However,
if the variable @code{scroll-step} has a nonzero value, an attempt is made
to scroll the buffer by that many lines; if that is enough to bring point
back into visibility, that is what is done.

@kindex{C-x <}
@kindex{C-x >}
@cfindex{scroll-left}
@cfindex{scroll-right}
@cindex{horizontal scrolling}
  The text in a window can also be scrolled horizontally.  This means that
each line of text is shifted sideways in the window, and one or more
characters at the beginning of each line are not displayed at all.  When a
window has been scrolled horizontally in this way, text lines are truncated
rather than continued (@pxref[Continuation Lines]), with a @samp{$} appearing in
the first column when there is text truncated to the left, and in the last
column when there is text truncated to the right.

  The command @kbd{C-x <} (@code{scroll-left}) scrolls the selected window
to the left by one column, or @var[n] columns with argument @var[n].
@kbd{C-x >} (@code{scroll-right}) scrolls in right one or more columns.
The window cannot be scrolled any farther to the right once it is
displaying normally (with each line starting at the window's right margin);
attempting to do so has no effect.

@kindex{M-r}
@cfindex{move-to-window-line}
  The commands described above all change the position of point on the
screen, carrying the text with it.  Another command moves point the same
way but leaves the text fixed.  It is @kbd{Meta-r}
(@code{move-to-window-line}).  With no argument, it puts point at the
beginning of the line at the center of the window.  An argument is used to
specify the line to put point on, counting from the top if the argument is
positive, or from the bottom if it is negative.  Thus, @kbd{M-0 M-r} moves
point to the text at the top of the window.  @kbd{Meta-r} never causes any
text to move on the screen; it causes point to move with respect to the
screen and the text.

@node[Search,Fixit,Display,Top]

@chapter[Searching and Replacement]
@setref Search
@cindex{searching}

  Like other editors, Emacs has commands for searching for an occurrence of
a string.  The principal search command is unusual in that it is
@dfn[incremental]; it begins to search before you have finished typing the
search string.  There are also nonincremental search commands more like
those of other editors.

  Besides the usual @code{replace-string} command that finds all
occurrences of one string and replaces them with another, Emacs has a
fancy replacement command called @code{query-replace} which asks
interactively which occurrences to replace.

@section{Incremental Search}

  An incremental search begins searching as soon as you type the first
character of the search string.  As you type in the search string, Emacs
shows you where the string (as you have typed it so far) would be found. 
When you have typed enough characters to identify the place you want, you
can stop.  Depending on what you will do next, you may or may not need to
terminate the search explicitly with an @key(ESC) first.

@c WideCommands
@table 7
@item C-s
Search forward.
@item C-r
Search backward.
@end table

@kindex{C-s}
@kindex{C-r}
@cfindex{isearch-forward}
@cfindex{isearch-backward}
  The command to search is @kbd{C-s} (@code{isearch-forward}).
@kbd{C-s} reads characters from the keyboard and positions the cursor at
the first occurrence of the characters that you have typed.  If you type
@kbd{C-s} and then @kbd{F}, the cursor moves right after the first
@samp{F}.  Type an @kbd{O}, and see the cursor move to after the first
@samp{FO}.  After another @kbd{O}, the cursor is after the first @samp{FOO}
after the place where you started the search.  Meanwhile, the search string
@samp{FOO} has been echoed in the echo area.

  If you make a mistake in typing the search string, you can erase
characters with @key(DEL).  Each @key(DEL) cancels the last character
of search string.  This does not happen until Emacs is ready to read
another input character; first it must either find, or fail to find, the
character you want to erase.  If you do not want to wait for this to happen,
use @kbd{C-g} as described below.

  When you are satisfied with the place you have reached, you can type
@key(ESC), which stops searching, leaving the cursor where the search
brought it.  Also, any command not specially meaningful in searches stops
the searching and is then executed.  Thus, typing @kbd{C-a} would exit the
search and then move to the beginning of the line.  @key(ESC) is
necessary only if the next command you want to type is a printing
character, @key(DEL), @key(ESC), or another control character that is
special within searches (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s} or
@kbd{C-k}).

  Sometimes you search for @samp{FOO} and find it, but not the one you
expected to find.  There was a second @samp{FOO} that you forgot about,
before the one you were looking for.  In this event, type another @kbd{C-s}
to move to the next occurrence of the search string.  This can be done any
number of times.  If you overshoot, you can cancel some @kbd{C-s}
characters with @key(DEL).

  After you exit a search, you can search for the same string again by
typing just @kbd{C-s C-s}: the first @kbd{C-s} is the key that invokes
incremental search, and the second @kbd{C-s} means ``search again''.

  If your string is not found at all, the echo area says @samp{Failing
I-Search}.  The cursor is after the place where Emacs found as much of your
string as it could.  Thus, if you search for @samp{FOOT}, and there is no
@samp{FOOT}, you might see the cursor after the @samp{FOO} in @samp{FOOL}.
At this point there are several things you can do.  If your string was
mistyped, you can rub some of it out and correct it.  If you like the place
you have found, you can type @key(ESC) or some other Emacs command to
``accept what the search offered''.  Or you can type @kbd{C-g}, which
removes from the search string the characters that could not be found (the
@samp{T} in @samp{FOOT}), leaving those that were found (the @samp{FOO} in
@samp{FOOT}).  A second @kbd{C-g} at that point cancels the search entirely,
returning point to where it was when the search started.

@cindex{quitting (in search)}
  The @kbd{C-g} ``quit'' character does special things during searches; just
what it does depends on the status of the search.  If the search has found
what you specified and is waiting for input, @kbd{C-g} cancels the entire
search.  The cursor moves back to where you started the search.  If
@kbd{C-g} is typed when there are characters in the search string that have
not been found---because Emacs is still searching for them, or because it
has failed to find them---then the search string characters which have not
been found are discarded from the search string.  With them gone, the
search is now successful and waiting for more input, so a second @kbd{C-g}
will cancel the entire search.

  To search for a control character such as @kbd{C-s} or @key(DEL) or
@key(ESC), you must quote it by typing @kbd{C-Q} first.  This function of
@kbd{C-Q} is analogous to its meaning as an Emacs command: it causes the
following character to be treated the way a graphic character would
normally be treated in the same context.

  You can change to searching backwards with @kbd{C-r}.  If a search fails
because the place you started was too late in the file, you should do this.
Repeated @kbd{C-r} keeps looking for more occurrences backwards.  A
@kbd{C-s} starts going forwards again.  @kbd{C-r} can be rubbed out just
like anything else.  If you know that you want to search backwards, you can
use @kbd{C-r} instead of @kbd{C-s} to start the search, because @kbd{C-r}
is also a key running a command (@code{isearch-reverse}) to search backward.

  The characters @kbd{C-y} and @kbd{C-w} can be used in incremental search
to grab text from the buffer into the search string.  This makes it
convenient to search for another occurrence of text at point.  @kbd{C-w}
grabs the word after point and makes it part of the search string;
@kbd{C-y} grabs the rest of the line onto the end of the search string.
Both commands advance over the text that is grabbed.  To search for the
next occurrence of the text that was grabbed, type @kbd{C-s}.

  All the characters special in incremental search can be changed by setting
the following variables:

@vindex{search-delete-char}
@vindex{search-exit-char}
@vindex{search-quote-char}
@vindex{search-repeat-char}
@vindex{search-reverse-char}
@vindex{search-yank-line-char}
@vindex{search-yank-word-char}
@table 3
@item @code{search-delete-char}
Character to delete from incremental search string (normally @key(DEL)).
@item @code{search-exit-char}
Character to exit incremental search (normally @key(ESC)).
@item @code{search-quote-char}
Character to quote special characters for incremental search (normally @kbd{C-q}).
@item @code{search-repeat-char}
Character to repeat incremental search forwards (normally @kbd{C-s}).
@item @code{search-reverse-char}
Character to repeat incremental search backwards (normally @kbd{C-r}).
@item @code{search-yank-line-char}
Character to pull rest of line from buffer into search string (normally @kbd{C-y}).
@item @code{search-yank-word-char}
Character to pull next word from buffer into search string (normally @kbd{C-w}).
@end table

@subsection{Slow Terminal Incremental Search}

  Incremental search on a slow terminal uses a modified style of
display that is designed to take less time.  Instead of redisplaying
the buffer at each place the search gets to, it creates a new single-line
window and uses that to display the line that the search has found.
The single-line window comes into play as soon as point gets outside
of the text that is already on the screen.

  When the search is terminated, the single-line window is removed.
Only at this time is the window in which the search was done
redisplayed to show its new value of point.

@vindex{isearch-slow-speed}
  The slow terminal style of display is used when the terminal baud
rate is less than or equal to the value of the variable
@code{isearch-slow-speed}, initially 1200.

@section{Nonincremental Search}
@cindex{nonincremental search}

  Emacs also has conventional nonincremental search commands, which
require you to type the entire search string before searching begins.

@table 7
@item C-s @key(ESC) @var[string] @key(RET)
Search for @var[string].
@item C-r @key(ESC) @var[string] @key(RET)
Search backward for @var[string].
@end table

  To do a nonincremental search, first type @kbd{C-s @key(ESC)}.
This enters the minibuffer to read the search string; terminate the
string with @key(RET), and then the search is done.  If the string is
not found the search command gets an error.

  The way @kbd{C-s @key(ESC)} works is that the @kbd{C-s} invokes
incremental search, which is specially programmed to invoke nonincremental
search if the argument you give it is empty.  (Such an empty argument would
otherwise be useless.)  @kbd{C-r @key(ESC)} also works this way.

@cfindex{search-forward}
@cfindex{search-backward}
  Forward and backward nonincremental searches are implemented by the commands
@code{search-forward} and @code{search-backward}.  These commands may be
bound to keys in the usual manner.  The reason that they are reached by
special-case code in incremental search is because @kbd{C-s @key(ESC)}
is the traditional sequence of characters used in Emacs to invoke
nonincremental search.

  However, nonincremental searches performed using @kbd{C-s @key(ESC)}
do not call @code{search-forward} right away.  The first thing done is to see
if the next character is @kbd{C-w}, which requests a word search.

@section{Word Search}
@cindex{word search}
@setref Word Search

  Word search searches for a sequence of words without regard to how the
words are separated.  More precisely, you type a string of many words,
using single spaces to separate them, and the string can be found even if
there are multiple spaces, newlines or other punctuation between the words.

  Word search is useful in editing documents formatted by text formatters.
If you edit while looking at the printed, formatted version, you can't tell
where the line breaks are in the source file.  With word search, you can
search without having to know them.

@table 7
@item C-s @key(ESC) C-w @var[words] @key(RET)
Search for @var[words], ignoring differences in punctuation.
@item C-r @key(ESC) C-w @var[words] @key(RET)
Search backward for @var[words], ignoring differences in punctuation.
@end table

  Word search is a special case of nonincremental search and is invoked
with @kbd{C-s @key(ESC) C-w}.  This is followed by the search string,
which must always be terminated with @key(RET).  Being nonincremental,
this search does not start until the argument is terminated.  It works
by constructing a regular expression and searching for that.
@xref[Regexp Search].

  A backward word search can be done by @kbd{C-r @key(ESC) C-w}.

@cfindex{word-search-forward}
@cfindex{word-search-backward}
  Forward and backward word searches are implemented by the commands
@code{word-search-forward} and @code{word-search-backward}.  These commands
may be bound to keys in the usual manner.  The reason that they are reached
by special-case code in incremental and nonincremental search is because
@kbd{C-s @key(ESC) C-w} is the traditional Emacs sequence of keys to use
to do a word search.

@node[Regexp Search,Replace,Search,Search]

@section[Regular Expression Search]
@setref Regexp Search
@cindex{regular expression}
@cindex{regexp}

@c ??? Documentation of regexp needed here

  A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern that
denotes a set of strings, possibly an infinite set.  Searching for matches
for a regexp is a very powerful operation that editors on Unix systems have
traditionally offered.  In GNU Emacs, you can search for the next match for
a regexp either incrementally or not.

@kindex{C-M-s}
@cfindex{isearch-forward-regexp}
  Incremental search for a regexp is done by typing @kbd{C-M-s}
(@code{isearch-forward-regexp}).  This command reads a search string
incrementally just like @kbd{C-s}, but it treats the search string as a
regexp rather than looking for an exact match against the text in the
buffer.  Each time you add text to the search string, you make the
regexp longer, and the new regexp is searched for.

@cfindex{re-search-forward}
@cfindex{re-search-backward}
  Nonincremental search for a regexp is done by the functions
@code{re-search-forward} and @code{re-search-backward}.  You can invoke
these with @kbd{M-x}, or bind them to keys.  Also, you can call
@code{re-search-forward} by way of incremental regexp search with
@kbd{C-M-s @key(ESC)}.

@section{Syntax of Regular Expressions}

Regular expressions have a syntax in which a few characters are special
constructs and the rest are @dfn{ordinary}.  An ordinary character is a
simple regular expression which matches that character and nothing else.
The special characters are @samp{$}, @samp{^}, @samp{.}, @samp{*}, @samp{[}, @samp{]} and @samp{\}.
Any other character appearing in a regular expression is ordinary,
unless a @samp{\} precedes it.@refill

For example, @samp{f} is not a special character, so it is ordinary,
and therefore @samp{f} is a regular expression that matches the string @samp{f}
and no other string.  (It does @i{not} match the string @samp{ff}.)  Likewise,
@samp{o} is a regular expression that matches only @samp{o}.

Any two regular expressions @var{a} and @var{b} can be concatenated.
The result is a regular expression which matches a string if @var{a}
matches some amount of the beginning of that string and @var{b}
matches the rest of the string.

As a simple example, we can concatenate the regular expressions
@samp{f} and @samp{o} to get the regular expression @samp{fo},
which matches only the string @samp{fo}.  Still trivial.

Note: for historical compatibility, special characters are treated as
ordinary ones if they are in contexts where their special meanings
make no sense.  For example, @samp{*foo} treats @samp{*} as ordinary since
there is no preceding expression on which the @samp{*} can act.
It is poor practice to depend on this behaviour; better to quote
the special character anyway, regardless of where is appears.

@node [directives, programming , syntax, top]

@ifinfo
The following are the characters and character sequences which have
special meaning within regular expressions.
Any character not mentioned here is not special; it stands for exactly
itself for the purposes of searching and matching.  @note [syntax]
@end ifinfo

@table 7
@item .
is a special character that matches anything except a newline.
Using concatenation, we can make regular expressions like @samp{a.b} which
matches any three-character string which begins with @samp{a} and ends with @samp{b}.@refill

@item *
is not a construct by itself; it is a suffix, which means the preceding
regular expression is to be repeated as many times as possible.  In
@samp{fo*}, the @samp{*} applies to the @samp{o}, so @samp{fo*} matches
@samp{f} followed by any number of @samp{o}s.  The case of zero
@samp{o}s is allowed: @samp{fo*} does match @samp{f}.

@samp{*} always applies to the @i{smallest} possible preceding expression.
Thus, @samp{fo*} has a repeating @samp{o}, not a repeating @samp{fo}.@refill

The matcher processes a @samp{*} construct by matching, immediately, as many
repetitions as can be found.  Then it continues with the rest of the
pattern.  If that fails, backtracking occurs, discarding some of
the matches of the @samp{*}-modified construct in case that makes it possible
to match the rest of the pattern.  For example, matching @samp{c[ad]*ar}
against the string @samp{caddaar}, the @samp{[ad]*} first matches @samp{addaa},
but this does not allow the next @samp{a} in the pattern to match.
So the last of the matches of @samp{[ad]} is undone and the following
@samp{a} is tried again.  Now it succeeds.@refill

@item [ ... ]
@samp{[} begins a @dfn{character set}, which is terminated by a @samp{]}.
In the simplest case, the characters between the two form the set.
Thus, @samp{[ad]} matches either @samp{a} or @samp{d},
and @samp{[ad]*} matches any string of @samp{a} and @samp{d}
(including the empty string), from which it follows that
@samp{c[ad]*r} matches @samp{car}, etc.@refill

Character ranges can also be included in a character set, by writing two
characters with a @samp{-} between them.  Thus, @samp{[a-z]} matches
any lower-case letter.  Ranges may be intermixed freely with
individual characters, as in @samp{[a-z$%.]}, which matches any
lower case letter or @samp{$}, @samp{%} or period.@refill

Note that the usual special characters are not special any more inside a
character set.  A completely different set of special characters exists
inside character sets: @samp{]}, @samp{-} and @samp{^}.@refill

To include a @samp{]} in a character set, you must make it
the first character.  For example, @samp{[]a]} matches @samp{]} or @samp{a}.
To include a @samp{-}, you must use it in a context where it cannot possibly
indicate a range: that is, as the first character, or immediately
after a range.@refill

@item [^ ... ]
@samp{[^} begins a @dfn{complement character set}, which matches any
character except the ones specified.  Thus, @samp{[^a-z0-9A-Z]}
matches all characters @i{except} letters and digits.@refill

@samp{^} is not special in a character set unless it is the first character.
The character following the @samp{^} is treated as if it were first
(it may be a @samp{-} or a @samp{]}).@refill

@item ^
is a special character that matches the empty string, but only
if at the beginning of a line in the text being matched.  Otherwise
it fails to match anything.  Thus, @samp{^foo} matches a @samp{foo}
which occurs at the beginning of a line.@refill

@item $
is similar to @samp{^} but matches only at the end of a line.
Thus, @samp{xx*$} matches a string of one @samp{x} or more
at the end of a line.@refill

@item \
has two functions: it quotes the above special characters
(including @samp{\}), and it introduces additional special constructs.@refill

Because @samp{\} quotes special characters, @samp{\$} is a regular
expression which matches only @samp{$}, and @samp{\[} is a regular
expression which matches only @samp{[}, and so on.@refill

For the most part, @samp{\} followed by any character matches only that
character.  However, there are several exceptions: characters which, when
preceded by @samp{\}, are special constructs.  Such characters are always
ordinary when encountered on their own.@refill

No new special characters will ever be defined.  All extensions to
the regular expression syntax are made by defining new two-character
constructs that begin with @samp{\}.@refill

@item \|
specifies an alternative.
Two regular expressions @var{a} and @var{b} with @samp{\|} in
between form an expression that matches anything that either @var{a} or
@var{b} will match.@refill

Thus, @samp{foo\|bar} matches either @samp{foo} or @samp{bar}
but no other string.@refill

@samp{\|} applies to the largest possible surrounding expressions.  Only a
surrounding @samp{\( ... \)} grouping can limit the grouping power of
@samp{\|}.@refill

Full backtracking capability exists to handle multiple uses of @samp{\|}.

@item \( ... \)
is a grouping construct that serves three purposes:

@enumerate
@item
To enclose a set of @samp{\|} alternatives for other operations.
Thus, @samp{\(foo\|bar\)x} matches either @samp{foox} or @samp{barx}.

@item
To enclose a complicated expression for the postfix @samp{*} to operate on.
Thus, @samp{ba\(na\)*} matches @samp{bananana}, etc., with any (zero or
more) number of @samp{na} strings.@refill

@item
To mark a matched substring for future reference.

@end enumerate

This last application is not a consequence of the idea of a parenthetical
grouping; it is a separate feature which happens to be assigned as a
second meaning to the same @samp{\( ... \)} construct because there is no
conflict in practice between the two meanings.  Here is an explanation
of this feature:@refill

@item \@var{digit}
after the end of a @samp{\( ... \)} construct, the matcher remembers the
beginning and end of the text matched by that construct.  Then, later on
in the regular expression, you can use @samp{\} followed by @var{digit}
to mean ``match the same text matched the @var{digit}'th time by the
@samp{\( ... \)} construct.''@refill

The strings matching the first nine @samp{\( ... \)} constructs appearing
in a regular expression are assigned numbers 1 through 9 in order of their
beginnings.
@samp{\1} through @samp{\9} may be used to refer to the text matched by
the corresponding @samp{\( ... \)} construct.@refill

For example, @samp{\(.*\)\1} matches any string that is composed of two
identical halves.  The @samp{\(.*\)} matches the first half, which may be
anything, but the @samp{\1} that follows must match the same exact text.@refill

@item \`
matches the empty string, but only if it is at the beginning
of the buffer.@refill

@item \'
matches the empty string, but only if it is at the end of
the buffer.@refill

@item \b
matches the empty string, but only if it is at the beginning or
end of a word.  Thus, @samp{\bfoo\b} matches any occurrence of
@samp{foo} as a separate word.  @samp{\bball\(s\|\)\b} matches
@samp{ball} or @samp{balls} as a separate word.@refill

@item \B
matches the empty string, provided it is @i{not} at the beginning or
end of a word.@refill

@item \<
matches the empty string, provided it is at the beginning of a word.

@item \>
matches the empty string, provided it is at the end of a word.

@item \w
matches any word-constituent character.
The editor syntax table determines which characters these are.

@item \W
matches any character that is not a word-constituent.

@item \s@var{code}
matches any character whose syntax is @var{code}.
@var{code} is a letter which represents a syntax code:
thus, @samp{w} for word constituent, @samp{-} for
whitespace, @samp{(} for open-parenthesis, etc.
@xref[Syntax].

@item \S@var{code}
matches any character whose syntax is not @var{code}.
@end table

@section{Searching and Case}

@vindex{case-fold-search}
@vindex{default-case-fold-search}
  All sorts of searches in Emacs normally ignore the case of the text they
are searching through; if you specify searching for @samp{FOO}, then
@samp{Foo} and @samp{foo} are also considered a match.  If you do not want
this feature, set the variable @code{case-fold-search} to @code{nil}.
This variable has separate values in all individual buffers; in a new
buffer, its value is initialized from @code{default-case-fold-search}.
@xref[Variables].

@node[Replace,?,Regexp Search,Search]

@section[Replacement Commands]
@setref Replace
@cindex{replacement}
@cindex{string substitution}
@cindex{global substitution}

  Global search-and-replace operations are not needed as often in Emacs as
they are in other editors, but they are available.  In addition to the
simple @code{replace-string} command which is like that found in most
editors, there is a @code{query-replace} command which asks you, for each
occurrence of the pattern, whether to replace it.

  The replace commands all replace one string (or regexp) with one
replacement string.  It is possible to perform several replacements in
parallel using the command @code{expand-region-abbrevs}.  @xref[Controlling
Expansion].

@subsection[Unconditional Replacement]
@cfindex{replace-string}
@cfindex{replace-regexp}

@table 7
@item M-x replace-string
Replace every occurrence of @var{string} with @var{newstring}.
@item M-x replace-regexp
Replace every match for @var{regexp} with @var{newstring}.
@end table

  To replace every instance of @samp{foo} after point with @samp{bar}, use
the command @kbd{M-x replace-string} with the two arguments @samp{foo} and
@samp{bar}.  Replacement occurs only after point, so if you want to cover
the whole buffer you must go to the beginning first.  All occurrences up to
the end of the buffer are replaced; to limit replacement to part of the
buffer, narrow to that part of the buffer before doing the replacement.

  When @code{replace-string} exits, point is left at the last occurrence
replaced.  The value of point when the @code{replace-string} command was
issued is remembered on the mark ring; @kbd{C-u C-@key(SPC)} moves back
there.

  @code{replace-string} replaces exact matches for a single string.  The
similar command @code{replace-regexp} replaces any match for a specified
pattern.

  In @code{replace-regexp}, the @var{newstring} need not be constant.  It can
refer to all or part of what is matched by the @var[regexp].  @samp{\&}
in @var[newstring] is replaced by the entire text being replaced.  @samp{\@var[d]}
in @var[newstring], where @var[d] is a digit, is replaced by whatever
matched the @var[d]'th parenthesized grouping in @var[regexp].  For example,
@example
M-x replace-regexp @key(RET) c[ad]+r @key(RET) \&-safe @key(RET)
@end example
@nopara
would replace (for example) @samp{cadr} with @samp{cadr-safe} and
@samp{cddr} with @samp{cddr-safe}.
@example
M-x replace-regexp @key(RET) \(c[ad]+r\)-safe @key(RET) \1 @key(RET)
@end example
@nopara
would perform exactly the opposite replacements.

  A numeric argument to either of the @code{replace-} commands restricts
replacement to matches that are surrounded by word boundaries.

@vindex{case-replace}
@vindex{case-fold-search}
  If the arguments to @code{replace-string} are in lower case, it preserves
case when it makes a replacement.  Thus, the command
@example
M-x replace-string @key(RET) foo @key(RET) bar @key(RET)
@end example
@nopara
replaces a lower case @samp{foo} with a lower case @samp{bar}, @samp{FOO}
with @samp{BAR}, and @samp{Foo} with @samp{Bar}.  If upper case letters
are used in the second argument, they remain upper case every time that
argument is inserted.  If upper case letters are used in the first
argument, the second argument is always substituted exactly as given, with
no case conversion.  Likewise, if the variable @code{case-replace} is set
to @code{nil}, replacement is done without case conversion.  If
@code{case-fold-search} is set to @code{nil}, case is significant in
matching occurrences of @samp{foo} to replace; also, case conversion of
the replacement string is not done.

@subsection[Query Replace]
@cindex{Query Replace}

@table 7
@item M-%
@itemx M-x query-replace
Replace some occurrences of one string with another string.
@item M-x query-replace-regexp
Replace some matches for a regexp with a specified string.
@end table

@kindex{M-%}
@cfindex{query-replace}
  If you want to change only some of the occurrences of @samp{foo} to
@samp{bar}, not all of them, then you cannot use an ordinary
@code{replace-string}.  Instead, use @kbd{M-%} (@code{query-replace}).
This command finds occurrences of @samp{foo} one by one, displays each
occurrence and asks you whether to replace it.  A numeric argument to
@code{query-replace} tells it to consider only occurrences of @samp{foo}
that are bounded by word-delimiter characters.

  Aside from querying, @code{query-replace} works just like
@code{replace-string}, and @code{query-replace-regexp} works
just like @code{replace-regexp}.

  The things you can type when you are shown an occurrence of @samp{foo}
are:

@kindex{SPC}
@kindex{DEL}
@kindex{Comma}
@kindex{ESC}
@kindex{.}
@kindex{!}
@kindex{C-r}
@kindex{C-w}
@kindex{C-l}

@c WideCommands
@table 7
@item @key(SPC)
to replace the @samp{foo} with @samp{bar}.  This preserves case, just like
@code{replace-string}, provided @code{case-replace} is non-@code{nil}, 
as it normally is.

@item @key(DEL)
to skip to the next @samp{foo} without replacing this one.

@item ,
to replace this @samp{foo} and display the result.  You are then asked for
another input character, except that since the replacement has already been
made, @key(DEL) and @key(SPC) are equivalent.

@item @key(ESC)
to exit without doing any more replacements.

@item .
to replace this @samp{foo} and then exit.

@item !
to replace all remaining occurrences of @samp{foo} without asking again.

@item ^
to go back to the location of the previous @samp{foo} (or what used to be
a @samp{foo}), in case changed it by mistake.  This works by popping the
mark ring.  Only one @kbd{^} is allowed, because only one previous
replacement location is kept during @code{query-replace}.

@item C-r
to enter a recursive editing level, in case the @samp{foo} needs to be
edited rather than just replaced with a @samp{bar}.  When you are done,
exit the recursive editing level with @kbd{C-M-c} and the next @samp{foo}
will be displayed.  @xref[Recursive Edit].

@item C-w
to delete the @samp{foo}, and then start editing the buffer.  When you are
finished editing whatever is to replace the @samp{foo}, exit the recursive
editing level with @kbd{C-M-c} and the next @samp{foo} will be displayed.

@item C-l
to redisplay the screen and then give another answer.
@end table

  If you type any other character, the @code{query-replace} is exited, and
the character executed as a command.  To restart the @code{query-replace},
use @kbd{C-x @key(ESC)}, which repeats the @code{query-replace} because
it used the minibuffer to read its arguments.  @xref[Repetition, C-x ESC].
  
@section[Other Search-and-Loop Commands]

  Here are some other commands that find matches for a regular expression.
They all operate from point to the end of the buffer.

@cfindex{list-matching-lines}
@cfindex{count-occurrences}
@cfindex{delete-non-matching-lines}
@cfindex{delete-matching-lines}
@c grosscommands
@table 7
@item M-x list-matching-lines
Print each line that follows point and contains a match for the specified
regexp.  A numeric argument specifies the number of context lines to print
before and after each matching line; the default is none.

@item M-x count-occurrences
Print the number of matches following point for the specified regexp.

@item M-x delete-non-matching-lines
Delete each line that follows point and does not contain a match for the
specified regexp.

@item M-x delete-matching-lines
Delete each line that follows point and contains a match for the specified
regexp.
@end table

@node[Fixit,Files,Search,Top]

@chapter[Commands for Fixing Typos]
@setref Fixit
@cindex{typos}

  In this chapter we describe the commands that are especially useful for
the times when you catch a mistake in your text just after you have made
it, or change your mind while composing text on line.

@c doublewidecommands
@table 7
@item @key(DEL)
Delete last character.
@item M-@key(DEL)
Kill last word.
@item C-x @key(DEL)
Kill to beginning of sentence.
@item C-t
Transpose two characters.
@item M-t
Transpose two words.
@item C-M-t
Transpose two balanced expressions.
@item C-x C-t
Transpose two lines.
@item M-- M-l
Convert last word to lower case.  @kbd{Meta--} is Meta-minus!
@item M-- M-u
Convert last word to all upper case.
@item M-- M-c
Convert last word to lower case with capital initial.
@item M-$
Check and correct spelling of word.
@item M-x spell-buffer
Check and correct spelling of each word in the buffer.
@item M-x spell-region
Check and correct spelling of each word in the region.
@item M-x spell-string
Check spelling of specified word.
@end table

@section[Killing Your Mistakes]

@kindex{DEL}
@cfindex{delete-backward-char}
  The @key(DEL) character (@code{delete-backward-char}) is the most
important correction command.  When used among graphic (self-inserting)
characters, it can be thought of as canceling the last character typed.

@kindex{M-DEL}
@kindex{C-x DEL}
@cfindex{backward-kill-word}
@cfindex{backward-kill-sentence}
  When your mistake is longer than a couple of characters, it might be more
convenient to use @kbd{M-@key(DEL)} or @kbd{C-x @key(DEL)}.
@kbd{M-@key(DEL)} kills back to the start of the last word, and @kbd{C-x
@key(DEL)} kills back to the start of the last sentence.  @kbd{C-x
@key(DEL)} is particularly useful when you are thinking of what to write
as you type it, in case you change your mind about phrasing.
@kbd{M-@key(DEL)} and @kbd{C-x @key(DEL)} save the killed text for
@kbd{C-y} and @kbd{M-y} to retrieve.  @xref[Yanking].

  @kbd{M-@key(DEL)} is often useful even when you have typed only a few
characters wrong, if you know you are confused in your typing and aren't
sure exactly what you typed.  At such a time, you cannot correct with
@key(DEL) except by looking at the screen to see what you did.  It
requires less thought to kill the whole word and start over again.

@section[Transposing Text]
@setref Transposition

@cindex{transposition}
@kindex{C-t}
@cfindex{transpose-chars}
  The common error of transposing two characters can be fixed, when they
are adjacent, with the @kbd{C-t} command (@code{transpose-chars}).
Normally, @kbd{C-t} transposes the two characters on either side of point.
When given at the end of a line, rather than transposing the last character
of the line with the newline, which would be useless, @kbd{C-t} transposes
the last two characters on the line.  So, if you catch your transposition
error right away, you can fix it with just a @kbd{C-t}.  If you don't catch
it so fast, you must move the cursor back to between the two transposed
characters.  If you transposed a space with the last character of the word
before it, the word motion commands are a good way of getting there.
Otherwise, a reverse search (@kbd{C-r}) is often the best way.
@xref[Search].


@kindex{C-x C-t}
@cfindex{transpose-lines}
@kindex{M-t}
@cfindex{transpose-words}
@kindex{C-M-t}
@cfindex{transpose-sexps}
  @kbd{Meta-t} (@code{transpose-words}) transposes the word before point
with the word after point.  It moves point forward over a word, dragging
the word preceding or containing point forward as well.  The punctuation
characters between the words do not move.  For example, @w{@samp[FOO, BAR]}
transposes into @w{@samp[BAR, FOO]} rather than @samp{@w[BAR FOO,]}.

  @kbd{C-M-t} (@code{transpose-sexps}) is a similar command for
transposing two expressions (@pxref[Lists]), and @kbd {C-x C-t}
(@code{transpose-lines}) exchanges lines.  They work like @kbd{M-t} except
in determining the division of the text into syntactic units.

  A numeric argument to a transpose command serves as a repeat count:
it tells the transpose command to move the character (word, sexp, line)
before or containing point across several other characters (words, sexps,
lines).  For example, @kbd{C-u 3 C-t} moves the character before point
forward across three other characters.  This is equivalent to repeating
@kbd{C-t} three times.  @kbd{C-u - 4 M-t} moves the word before point
backward across four words.  @kbd{C-u - C-M-t} would cancel the effect of
plain @kbd{C-M-t}.

  A numeric argument of zero is assigned a special meaning (because
otherwise a command with a repeat count of zero would do nothing):
to transpose the character (word, sexp, line) before point with the one
before the mark.

@section[Case Conversion]

@cfindex{downcase-word}
@cfindex{upcase-word}
@cfindex{capitalize-word}
@kindex{M-- M-l}
@kindex{M-- M-u}
@kindex{M-- M-c}
@cindex{case conversion}
@cindex{words}
  A very common error is to type words in the wrong case.  Because of this,
the word case-conversion commands @kbd{M-l}, @kbd{M-u} and @kbd{M-c} have a
special feature when used with a negative argument: they do not move the
cursor.  As soon as you see you have mistyped the last word, you can simply
case-convert it and go on typing.  @xref[Case].

@section[Checking and Correcting Spelling]
@cindex{spelling}

@kindex{M-$}
@cfindex{spell-word}
  To check the spelling of the word before point, and optionally correct it
as well, use the command @kbd{M-$} (@code{spell-word}).  This command runs
an inferior process containing the @code{spell} program to see whether the
word is correct English.  If it is not, it asks you to edit the word (in
the minibuffer) into a corrected spelling, and then does a
@code{query-replace} to substitute the corrected spelling for the old one
throughout the buffer.

  If you exit the minibuffer without altering the original spelling, it
means you do not want to do anything to that word.  Then the
@code{query-replace} is not done.

@cfindex{spell-buffer}
  @kbd{M-x spell-buffer} checks each word in the buffer the same way that
@code{spell-word} does, doing a @code{query-replace} if appropriate for
every incorrect word.

@cfindex{spell-region}
  @kbd{M-x spell-region} is similar but operates only on the region, not
the entire buffer.

@cfindex{spell-string}
  @kbd{M-x spell-string} reads a string as an argument and checks whether
that is a correctly spelled English word.  It prints in the echo area a
message giving the answer.

@node[Files,Buffers,Fixit,Top]

@chapter[File Handling]
@setref Files
@cindex{files}

  The basic unit of stored data is the file.  Each program, each paper,
lives usually in its own file.  To edit a program or paper, you must tell
Emacs to examine the file and prepare a buffer containing a copy of the
file's text.  This is called @dfn[visiting] the file.  Editing commands
apply directly to text in the buffer; that is, to the copy inside Emacs.
Your changes only appear in the file itself when you @dfn{save} the buffer
back into the file.

  In addition to visiting and saving files, Emacs can delete, copy, rename,
and append to files, and operate on file directories.

@section File Names
@setref File Names
@cindex{file names}

  Most Emacs commands that operate on a file require you to specify the
file name.  (Saving and reverting are exceptions; the buffer knows which
file name to use for them.)  File names are specified using the minibuffer
(@pxref[Minibuffer]).  @dfn{Completion} is available, to make it easier to
specify long file names.  @xref[Completion].

  There is always a @dfn{default file name} which will be used if you type
just @key(RET), entering an empty argument.  Normally the default file
name is the name of the file visited in the current buffer; this makes it
easy to operate on that file with any of the Emacs file commands.

@vindex{default-directory}
  Each buffer has a default directory, normally the same as the
directory of the file visited in that buffer.  When Emacs reads a file
name, if you do not specify a directory, the default directory is
used.  If you specify a directory in a relative fashion, with a name
that does not start with a slash, it is interpreted with respect to
the default directory.  The default directory is kept in the variable
@code{default-directory}, which has a separate value in every buffer.

  For example, if the default file name is @code{/u/rms/gnu/gnu.tasks} then
the default directory is @code{/u/rms/gnu/}.  If you type just @code{foo},
which does not specify a directory, it is short for @code{/u/rms/gnu/foo}.
@code{../.login} would stand for @code{/u/rms/.login}.  @code{new/foo}
would stand for @code{/u/rms/gnu/new/foo}.

  The default directory actually appears initially in the minibuffer when
the file name is read.  This serves two purposes: it shows you what the
default is, so that you can type a relative file name and know with
certainty what it will mean, and it allows you to edit the default to
specify a different directory.

  Note that it is legitimate to type an absolute file name after you
enter the minibuffer, ignoring the presence of the default directory name
as part of the text.  The final minibuffer contents may look invalid, but
that is not so.  @xref[Minibuffer File].

  The command @kbd{M-x pwd} prints the current buffer's default directory,
and the command @kbd{M-x cd} sets it (to a value read using the minibuffer).
A buffer's default directory changes only when the @code{cd} command is used.
A file-visiting buffer's default directory is initialized to the directory
of the file that is visited there.  If a buffer is made randomly with
@kbd{C-x b}, its default directory is copied from that of the buffer that
was current at the time.

@node[Visiting,Revert,Files,Files]

@section[Visiting Files]
@setref Visiting
@cindex{visiting files}

@c WideCommands
@table 7
@item C-x C-f
Visit a file.
@item C-x C-v
Visit a different file instead of the one visited last.
@item C-x 4 C-f
Visit a file, in another window.  Don't change this window.
@end table

@cindex{files}
@cindex{visiting}
@cindex{saving}
@vindex{ask-about-buffer-names}
  @dfn[Visiting] a file means copying its contents into Emacs where you can
edit them.  Emacs makes a new buffer for each file that you visit.  We say
that the buffer is visiting the file that it was created to hold.
Emacs constructs the buffer name from the file name by throwing away the
directory, keeping just the name proper.  For example, a file named
@code{/usr/rms/emacs.tex} would get a buffer named @samp{emacs.tex}.  If
there is already a buffer with that name, a unique name is constructed by
appending @samp{<2>}, @samp{<3>}, or so on, using the lowest number that  
makes a name that is not already in use.  If the variable
@code{ask-about-buffer-names} is non-@code{nil}, the user is asked what
buffer name to use; this takes the place of automatic uniquization.

  Since the current buffer name always appears in the mode line, you can
tell instantly from it which file you are editing.

  The changes you make with Emacs are made in the Emacs buffer.  They do
not take effect in the file that you visited, or any place permanent, until
you @dfn{save} the buffer.  Saving the buffer means that Emacs writes the
current contents of the buffer into its visited file.  @xref[Saving].

@cindex{modified (buffer)}
  If a buffer contains changes that have not been saved, the buffer is said
to be @dfn{modified}.  This is important because it implies that some
changes will be lost if the buffer is not saved.  The mode line displays
two stars near the left margin if the current buffer is modified.

@kindex{C-x C-f}
@cfindex{find-file}
  To visit a file, use the command @kbd{C-x C-f} (@code{find-file}).
Follow the command with the name of the file you wish to visit, terminated
by a @key(RET).

  The file name is read using the minibuffer (@pxref[Minibuffer]), with
defaulting and completion in the standard manner (@pxref[File Names]).
While in the minibuffer, you can abort @kbd{C-x C-f} by typing @kbd{C-g}.

  Your confirmation that @kbd{C-x C-f} has completed successfully is the
appearance of new text on the screen and a new buffer name in the mode
line.  If the specified file does not exist and could not be created, or
cannot be read, then an error results.  The error message is printed in
the echo area, and includes the file name which Emacs was trying to visit.

  If you visit a file that is already in Emacs, @kbd{C-x C-f} does not
make another copy.  It selects the existing buffer containing that file.
However, before doing so, it checks that the file itself has not changed
since you visited or saved it last.  If the file has changed, a warning
message is printed.  @xref[Interlocking,,Simultaneous Editing].

@cindex{creating files}
  What if you want to create a file?  Just visit it.  Emacs prints
@w{@samp[(New File)]} in the echo area, but in other respects behaves as if
you had visited an existing empty file.  If you make any changes and save
them, the file is created.

@kindex{C-x C-v}
@cfindex{find-alternate-file}
  If you visit a nonexistent file unintentionally (because you typed the
wrong file name), use the @kbd{C-x C-v} (@code{find-alternate-file})
command to visit the file you wanted.  @kbd{C-x C-v} is similar to @kbd{C-x
C-f}, but it kills the current buffer (after first offering to save it
if it is modified).

@vindex{find-file-run-dired}
  If the file you specify is actually a directory, Dired is called on that
directory (@pxref[Dired]).  This can be inhibited by setting the variable
@code{find-file-run-dired} to @code{nil}; then it is an error to try to
visit a directory.

@kindex{C-x 4 f}
@cfindex{find-file-other-window}
  @kbd{C-x 4 f} (@code{find-file-other-window}) is like @kbd{C-x C-f} except
that the buffer containing the specified file is selected in another window.
The window that was selected before @kbd{C-x 4 f}
continues to show the same buffer it was already showing.  If this command
is used when only one window is being displayed, that window is split in two,
with one window showing the same before as before, and the other one
showing the newly requested buffer.

@node[Saving,Reverting,Visiting,Files]

@section{Saving Files}
@setref Saving

  @dfn{Saving} a buffer in Emacs means writing its contents back into the file
that was visited in the buffer.

@table 7
@item C-x C-s
Save the current buffer in its visited file.
@item C-x s
Save any or all buffers in their visited files.
@item M-~
Forget that the current buffer has been changed.
@item C-x C-w
Save the current buffer in a specified file, and record that
file as the one visited in the buffer.
@item M-x set-visited-file-name
Mark the current buffer as visiting a specified file.
@end table

@kindex{C-x C-s}
@cfindex{save-buffer}
  When you wish to save the file and make your changes permanent, type
@kbd{C-x C-s} (@code{save-buffer}).  After saving is finished, @kbd{C-x C-s}
prints a message such as
@example
Wrote /u/rms/gnu/gnu.tasks
@end example
@nopara
If the selected
buffer is not modified (no changes have been made in it since the buffer
was created or last saved), saving is not really done, because it would be
redundant.  Instead, @kbd{C-x C-s} prints a message in the echo area saying
@example
(No changes need to be written)
@end example

@kindex{C-x s}
@cfindex{save-some-buffers}
  The command @kbd{C-x s} (@code{save-some-buffers} can save any or all modified
buffers.  First it asks, for each modified buffer, whether to save it.
These questions appear as typeout, overlying the buffer text, and should
be answered with @kbd{Y} or @kbd{N}.  After all questions have been asked,
the buffers you have approved are all saved.

@kindex{M-~}
@cfindex{not-modified}
  If you have changed a buffer and do not want the changes to be saved, you
should take some action to prevent it.  Otherwise, each time you use
@code{save-some-buffers} you are liable to save it by mistake.  One thing
you can do is type @kbd{M-~} (@code{not-modified}), which clears out the
indication that the buffer is modified.  If you do this, none of the save
commands will believe that the buffer needs to be saved.  (If we take
@samp{~} to mean `not', then @kbd{Meta-~} is `not', metafied.)  You could
also use @code{set-visited-file-name} (see below) to mark the buffer as
visiting a different file name, one which is not in use for anything important.
Alternatively, you can undo all the changes made since the file was visited
or saved, by reading the text from the file again.  This is called
@dfn{reverting}.  @xref[Reverting].  You could also undo all the changes by
repeating the undo command @kbd{C-x u} until you have undone all the changes;
but this only works if you have not made more changes than the undo mechanism
can remember.

@cfindex{set-visited-file-name}
  @kbd{M-x set-visited-file-name} alters the name of the file that the
current buffer is visiting.  It reads the new file name using the
minibuffer.  It can be used on a buffer that is not visiting a file, too.
The buffer's name is changed to correspond to the file it is now visiting
in the usual fashion (unless the new name is in use already for some other
buffer; in that case, the buffer name is not changed).
@code{set-visited-file-name} does not save the buffer in the newly
visited file; it just alters the records inside Emacs so that, if you
save the buffer, it will be saved in that file.  It also marks the buffer
as ``modified'' so that @kbd{C-x C-s} @i{will} save.

@kindex{C-x C-w}
@cfindex{write-file}
  If you wish to mark the buffer as visiting different file and save it
right away, use @kbd{C-x C-w} (@code{write-file}).  It is precisely
equivalent to @code{set-visited-file-name} followed by @kbd{C-x C-s}.
@kbd{C-x C-s} used on a buffer that is not visiting with a file has the
same effect as @kbd{C-x C-w}; that is, it reads a file name, marks the
buffer as visiting that file, and saves it there.  The default file name in a
buffer that is not visiting a file is made by combining the buffer name (as
the name component of the pathname) with the other components taken from
the last file-visiting buffer that was current.

  If Emacs is about to save a file and sees that the date of the latest
version on disk does not match what Emacs last read or wrote, Emacs
notifies you of this fact, because it probably indicates a problem caused
by simultaneous editing and requires your immediate attention.
@xref[Interlocking, Simultaneous Editing].@refill

@vindex{require-final-newline}
  If the variable @code{require-final-newline} is non-@code{nil},
Emacs puts a newline at the end of any file that doesn't already end in
one, every time a file is saved or written.

@node[Backup,Interlocking,Saving,Saving]

@section{Backup Files}
@setref Backup
@cindex{backup file}

  Because Unix does not provide version numbers in file names, rewriting a
file in Unix automatically destroys all record of what the file used to
contain.  Thus, saving a file from Emacs throws away the old contents of
the file--or it would, except that Emacs carefully copies the old contents
to another file, called the @dfn{backup} file, before actually saving.
The backup file's name is constructed by appending @samp{~} to the file
name being edited; thus, the backup file for @code{eval.c} would be
@code{eval.c~}.

  Emacs makes a backup for a file only the first time the file is saved in
each editing session.  No matter how many times you save a file, its backup
file continues to contain the contents from before the current editing session.

  Backup files can be made by copying the old file or by renaming it.
This makes a difference when the old file has multiple names.  If the
old file is renamed into the backup file, then the alternate names become
names for the backup file.  If the old file is copied instead, then the
alternate names remain names for the file that you are editing, and
the contents accessed by those names will be the new contents.

@vindex{backup-by-copying}
@vindex{backup-by-copying-when-linked}
  The choice of renaming or copying is controlled by two variables.
Normally, renaming is done.  If the variable @code{backup-by-copying} is
non-@code{nil}, copying is used.  If the variable
@code{backup-by-copying-when-linked} is non-@code{nil}, then copying is
done for files that have multiple names, but renaming is done when the file
being edited has only one name.  (For files with only one name,
the major difference between renaming and copying is that renaming is faster.)

@vindex{make-backup-files}
  If the variable @code{make-backup-files} is set to @code{nil}, backup
files are not written at all.

@node[Interlocking,,Backup,Saving]

@subsection{Protection against Simultaneous Editing}
@setref Interlocking

@cindex{file dates}
@cindex{simultaneous editing}
  Simultaneous editing occurs when two users visit the same file, both make
changes, and then both save them.  If nobody were informed that this was
happening, whichever user saved first would later find that his changes
were lost.  Emacs cannot prevent users from editing simultaneously, but it
always warns at least one of the users (the one who saves last) that he is
about to lose.  If he takes the proper corrective action at this point, he
can prevent a problem.

  Every time Emacs saves a buffer, it first checks the
last-modification-date of the existing file on disk to see that it has not
changed since the file was last visited or saved.  If the date does not
match, it implies that changes were made in the file in some other way, and
these changes are about to be lost if Emacs actually does save.  To prevent
this, Emacs prints a warning message and asks for confirmation before
saving.  Occasionally you will know why the file was changed and know that
it does not matter; then you can answer `yes' and proceed.  Otherwise, you
should cancel the save with @kbd{C-g} and investigate the situation.

  The first thing you should do when notified of simultaneous editing is
list the directory with @kbd{C-u C-x C-d} (@pxref[ListDir,,Directory Listing]).
This will show the file's current author.  You should attempt to contact
him to warn him not to continue editing.  Often the next step is to save
the contents of your Emacs buffer under a different name, and use
@code{diff} to compare the two files.

  Simultaneous editing checks are also made when you visit with @kbd{C-x
C-f} a file that is already visited.  This is not strictly necessary, but
it can cause you to find out about the problem earlier, when perhaps
correction takes less work.

@node[Reverting,Auto Save,Saving,Files]

@section[Reverting a Buffer]
@setref Reverting
@cfindex{revert-buffer}@cindex{drastic changes}

  If you have made extensive changes to a file and then change your mind
about them, you can get rid of them by reading in the previous version of
the file.  To do this, use @kbd{M-x revert-buffer}, which operates on the
current buffer.  Since this is a very dangerous thing to do, you must
confirm it with `yes'.

  If the current buffer has been auto-saved more recently than it has been
saved for real, @code{revert-buffer} offers to read the auto save file
instead of the visited file.  This question comes before the usual request
for confirmation, and demands @kbd{y} or @kbd{n} as an answer.  If you have
started to type @kbd{yes} for confirmation without realizing that the other
question was going to be asked, the @kbd{y} will answer that question, but
the @kbd{es} will not be valid confirmation.  So you will have a chance to
cancel the operation with @kbd{C-g} and try it again with the answers that
you really intend.

  @code{revert-buffer} keeps point at the same distance (measured in
characters) from the beginning of the file.  If the file was edited only
slightly, you will be at approximately the same piece of text after
reverting as before.  If you have made drastic changes, the same value of
point in the old file may address a totally different piece of text.

  A reverted buffer is marked ``not modified'' until another change is made.

  Some kinds of buffers whose contents reflect data bases other than files,
such as Dired buffers, can also be reverted.  For them, reverting means
recalculating their contents from the appropriate data base.  Buffers
created randomly with @kbd{C-x b} cannot be reverted; @code{revert-buffer}
reports an error when asked to do so.

@node[Auto Save,ListDir,Revert,Files]

@section[Auto Saving: Protection Against Disasters]
@setref Auto Save
@cindex{Auto Save mode}
@cindex{crashes}

  Emacs saves all the visited files from time to time (based on counting
your keystrokes) without being asked.  This is called @dfn{auto-saving}.
It prevents you from losing more than a limited amount of work if the
system crashes.

@vindex{auto-save-visited-file-name}
  Auto-saving does not normally save in the files that you visited, because
it can be very undesirable to save a program that is in an inconsistent
state because you have made half of a planned change.  Instead, auto-saving
is done in a different file called the @dfn{auto-save file}, and the
visited file is changed only when you request saving explicitly (such as
with @kbd{C-x C-s}).  If you want auto-saving to be done in the visited file,
set the variable @code{auto-save-visited-file-name} to be non-@code{nil}.
The file name to be used for auto-saving in a buffer is calculated when
auto-saving is turned on in that buffer, based on the variable values in
effect at that time.

  Normally, the auto-save file name is made by appending @samp{#} to the
front of the visited file.  Thus, a buffer visiting file @code{foo.c} would
be auto-saved in a file @code{#foo.c}.  Most buffers that are not visiting
files are auto-saved only if you request it explicitly; when they are
auto-saved, the auto-save file name is made by appending @samp{#%} to the
buffer name.  For example, the @code{*mail*} buffer in which you compose
messages to be sent is auto-saved in a file named @code{#%*mail*}.
Auto-save file names are made this way unless you reprogram parts of Emacs
to do something different.

@vindex{auto-save-default}
@cfindex{auto-save-mode}
  Each time you visit a file, no matter how, auto saving is turned on for
that file if the variable @code{auto-save-default} is non-@code{nil}.  The
default for this variable is @code{t}, so auto-saving is the usual practice
for file-visiting buffers.  Auto-saving can be turned on or off for any
existing buffer with the command @kbd{M-x auto-save-mode}.  Like other
minor mode commands, @kbd{M-x auto-save-mode} turns auto-saving on with a
positive argument, off with a zero or negative argument; with no argument,
it toggles.

@vindex{auto-save-interval}
@cfindex{do-auto-save}
  Emacs does auto-saving every so often, based on counting how many
characters you have typed since the last time auto-saving was done.  The
variable @code{auto-save-interval} specifies how many characters there are
between auto-saves.  By default, it is 300.  Emacs also auto-saves whenever
you call the function @code{do-auto-save}.

  Emacs also does auto-saving whenever it gets a fatal error.  This includes
killing the Emacs job with a shell command such as @code{kill %emacs}, or
disconnecting a phone line or network connection.

  When Emacs determines that it is time for auto-saving, each buffer is
considered, and is auto-saved if auto-saving is turned on for it and it has
been changed since the last time it was auto-saved.  If any auto-saving is
done, the message @samp{Auto-saving...} is displayed in the echo area until
auto-saving is finished.  Errors occurring during auto-saving are trapped
so that they do not interfere with the execution of commands you have been
typing.

@vindex{delete-auto-save-files}
  You may wish to have a buffer's auto-save file deleted when you save the
buffer in its visited file.  To request this, set the variable
@code{delete-auto-save-files} non-@code{nil}.  The variable is normally
@code{nil}, so that auto-save files remain until explicitly deleted.

  The way to use the contents of an auto save file to recover from a loss
of data is to make a buffer with @kbd{C-x b}, read the auto-save file into
it with @kbd{M-x insert-file}, and then write it into the desired file name
with @kbd{C-x C-w}.  For example, to recover file @code{foo.c} from its
auto-save file @code{#foo.c}, do:
@example
C-x b temp @key(RET)
M-x insert-file @key(RET) #foo.c @key(RET)
C-x C-w foo.c @key(RET)
@end example

@node[ListDir,Dired,Auto Save,Files]

@section[Listing a File Directory]
@setref ListDir

@cindex{file directory}
@cindex{directory listing}
  Files are classified into @dfn{directories}.  A @dfn{directory listing}
is a list of all the files in a directory.  Emacs provides directory
listings in brief format (file names only) and verbose format (sizes,
dates, and authors included).

@table 7
@item C-x C-d @var[dir-or-pattern]
Print a brief directory listing.
@item C-u C-x C-d @var[dir-or-pattern]
Print a verbose directory listing.
@end table

@cfindex{list-directory}
@kindex{C-x C-d}
  The command to print a directory listing is @kbd{C-x C-d}
(@code{list-directory}).  It reads using the minibuffer a file name which
is either a directory to be listed or a wildcard-containing pattern for the
files to be listed.  For example,
@example
C-x C-d /u2/emacs/etc @key(RET)
@end example
@nopara
lists all the files in directory @code{/u2/emacs/etc}.  An example of
specifying a file name pattern is
@example
C-x C-d /u2/emacs/src/*.c @key(RET)
@end example
@nopara
In accepting either form of argument, @kbd{C-x C-d} resembles the Unix
directory-listing command @code{ls}.

  Normally, @kbd{C-x C-d} prints a brief directory listing containing just
file names.  A numeric argument (regardless of value) tells it to print a
verbose listing (like @code{ls -l}).

@vindex{list-directory-brief-switches}
@vindex{list-directory-verbose-switches}
  The text of a directory listing is obtained by running @code{ls} in an
inferior process.  Two Emacs variables control the switches passed to
@code{ls}: @code{list-directory-brief-switches} is a string giving the
switches to use in brief listings (@code{"-CF"} by default), and
@code{list-directory-verbose-switches} is a string giving the switches to
use in a verbose listing (@code{"-l"} by default).

@node[Dired,Filadv,ListDir,Files]

@chapter[Dired, the Directory Editor]
@setref Dired
@cindex{Dired}
@cindex{deletion (of files)}

  Dired makes it easy to delete or visit many of the files in a single
directory at once.  It makes an Emacs buffer containing a listing of the
directory.  You can use the normal Emacs commands to move around in this
buffer, and special Dired commands to operate on the files.

@cfindex{dired}
@kindex{C-x d}
@vindex{dired-listing-switches}
  To invoke dired, do @kbd{C-x d} or @kbd{M-x dired}.  The command reads
a directory name or wildcard file name pattern as a minibuffer argument
just like the @code{list-directory} command, @kbd{C-x C-d}.  Where
@code{dired} differs from @code{list-directory} is in naming the buffer
after the directory name or the wildcard pattern used for the listing, and
putting the buffer into Dired mode so that the special commands of Dired
are available in it.  The variable @code{dired-listing-switches} is a
string used as an argument to @code{ls} in making the directory; this
string @i[must] contain @samp{-l}.

  Once the Dired buffer exists, you can switch freely between it and other
Emacs buffers.  Whenever the Dired buffer is selected, certain special
commands are provided that operate on files that are listed.  The Dired
buffer is ``read-only'', and inserting text in it is not useful, so
ordinary printing characters such as @kbd{d} and @kbd{x} are used for Dired
commands.  Most Dired commands operate on the file described by the line
that point is on.  Some commands perform operations immediately; others
``mark'' the file to be operated on later.

  Most Dired commands that operate on the current line's file also treat a
numeric argument a repeat count, meaning to apply to the files of the next
few lines.  A negative argument means to operate on the files of the
preceding lines, and leave point on the first of those lines.

  All the usual Emacs cursor motion commands are available in Dired
buffers.  Some special purpose commands are also provided.

  For extra convenience, @key(SPC) in Dired is a command similar to
@kbd{C-N}.  Moving down a line is done so often in Dired that it deserves
to be easy to type.  @key(DEL) is often useful simply for moving up.

@section{Deleting Files with Dired}

  The primary use of Dired is to mark files for deletion and then delete them.

@table 7
@item d
Mark this file for deletion.
@item u
Remove deletion-mark on this line.
@item @key(DEL)
Remove deletion-mark on previous line, moving point to that line.
@item x
Delete the files that are marked for deletion.
@item #
Mark all auto-save files (files whose names start with @samp{#}) for
deletion (@pxref[Auto Save]).
@item ~
Mark all backup files (files whose names end with @samp{~}) for deletion
(@pxref[Backup]).
@end table

  You can mark a file for deletion by moving to the line describing the
file and typing @kbd{d} or @kbd{C-d}.  The deletion mark is visible as a
@samp{D} at the beginning of the line.  Point is moved to the beginning of
the next line, so that repeated @kbd{d} commands mark successive files.

  The files are marked for deletion rather than deleted immediately to
avoid the danger of deleting a file accidentally.  Until you direct Dired
to delete the marked files, you can remove deletion marks using the
commands @kbd{u} and @key(DEL).  @kbd{u} works just like @kbd{d}, but
removes marks rather than making marks.  @key(DEL) moves upward,
removing marks; it is like @kbd{u} with numeric argument automatically
negated.

  To delete the marked files, type @kbd{x}.  This command first displays a
list of all the file names marked for deletion, and requests confirmation
with `yes'.  Once you confirm, all the marked files are deleted, and their
lines are deleted from the text of the Dired buffer.  The shortened Dired
buffer remains selected.  If you answer `no' or quit with @kbd{C-g}, you
return imediately to Dired, with the deletion marks still present and no
files actually deleted.

  The @kbd{#} and @kbd{~} commands marks many files for deletion, based on
their names.  @kbd{#} marks for deletion all files that appear to have been
made by auto-saving (that is, files whose names begin with @samp{#}).
@kbd{~} marks for deletion all files that appear to have been made as
backups for files that were edited (that is, files whose names end with
@samp{~}).  These commands are useful precisely because they do not
actually delete any files; you can remove the deletion marks from any
marked files that you really wish to keep.

@section{Immediate File Operations in Dired}

  Some file operations in Dired take place immediately when they are
requested.

@table 7
@item c
Copies the file described on the current line.  You must supply a file name
to copy to, using the minibuffer.
@item f
Visits the file described on the current line.  It is just like typing
@kbd{C-x C-f} and supplying that file name.  If the file on this line is a
subdirectory, @kbd{f} actually causes Dired to be invoked on that
subdirectory.  @xref[Visiting].
@item o
Like @kbd{f}, but uses another window to display the file's buffer.  The
Dired buffer remains visible in the first window.  This is like using
@kbd{C-x 4 C-f} to visit the file.  @xref[Windows].
@item r
Renames the file described on the current line.  You must supply a file
name to rename to, using the minibuffer.
@item v
Views the file described on this line using @kbd{M-x view-file}.  Viewing a
file is like visiting it, but is slanted toward moving around in the file
conveniently and does not allow changing the file.
@xref[Filadv,View File,Other File Operations].
@end table

@node[Filadv,,ListDir,Files]

@section[Miscellaneous File Operations]
@setref Filadv

  Emacs has extended commands for performing many other operations on
files.

@cfindex{view-file}@cindex{viewing}
  @kbd{M-x view-file} allows you to scan or read a file by sequential
screenfuls.  It reads a file name argument using the minibuffer.  After
reading the file into an Emacs buffer, @code{view-file}
reads and displays one window full.  You can then type
@key(SPC) to scroll forward one window full, or @key(DEL).  Various
other commands are provided for moving around in the file, but none for
changing it.  To exit from viewing, type @kbd{C-c}.

@cfindex{insert-file}
  @kbd{M-x insert-file} inserts the contents of the specified file into the
current buffer at point, leaving point unchanged before the contents and
an inactive mark after them.  @xref[Mark].

@cfindex{write-region}
@cfindex{append-to-file}
  @kbd{M-x write-region} is the inverse of @kbd{M-x insert-file};
it copies the contents of the region into the specified file.  @kbd{M-x
append-to-file} adds the text of the region to the end of the specified
file.

@cfindex{delete-file}
@cindex{deletion (of files)}
  @kbd{M-x delete-file} deletes the specified file, like the @code{rm}
command in the shell.  If you are deleting many files in one directory, it
may be more convenient to use Dired (@pxref[Dired]).

@cfindex{rename-file}
@cfindex{add-name-to-file}
  @kbd{M-x rename-file} reads two file names @var{old} and @var{new} using
the minibuffer, then renames file @var{old} as @var{new}.  If a file named
@var[new] already exists, you must confirm with `yes' or renaming is not done.
The similar command @kbd{M-x add-name-to-file} is used to add an additional
name to an existing file without removing its old name.

@cfindex{copy-file}
  @kbd{M-x copy-file} works just like @kbd{M-x rename-file}, but copies the
files @var{old} to the files @var{new}.  Confirmation is required if
@var[new] exists.

@cfindex{make-symbolic-link}
  @kbd{M-x make-symbolic-link} reads two file names @var[old] and @var[linkname],
and then creates a symbolic link named @var[linkname] and pointing at @var[old].
The effect is that future attempts to open file @var[linkname] will refer
to whatever file is named @var[old] at the time the opening is done, or
will get an error if the name @var[old] is not in use at that time.
Confirmation is required when creating the link if @var[linkname] is in use.
Note that not all systems support symbolic links.

@node[Buffers,Display,Files,Top]

@chapter[Using Multiple Buffers]
@setref Buffers

@cindex{buffers}
  The text you are editing in Emacs resides in an object called a
@dfn{buffer}.  Each time you visit a file, a buffer is created to hold the
file's text.  Each time you invoke Dired, a buffer is created to hold the
directory listing.  If you a message with @kbd{C-x m}, a buffer
named @code{*mail*} is used to hold the text of the message.  When you
ask for a command's documentation, that appears in a buffer called
@code{*Help*}.

@cindex{selected buffer}
@cindex{current buffer}
  At any time, one and only one buffer is @dfn{selected}.  It is also
called the @dfn{current buffer}.  Often we say that a command operates on
``the buffer'' as if there were only one; but really this means that the
command operates on the selected buffer (most commands do).

  When Emacs makes multiple windows, each window has a chosen buffer which
is displayed there, but at any time only one of the windows is selected
and its chosen buffer is the selected buffer.  Each window's mode line
displays the name of the buffer that the window is displaying.

  Each buffer has a name, which can be of any length, and you can select
any buffer by giving its name.  Most buffers are made by visiting files,
and their names are derived from the files' names.  But you can also create
an empty buffer with any name you want.  A newly started Emacs has a buffer named
@samp{*scratch*} which can be used for evaluating Lisp expressions in Emacs.
The distinction between upper and lower case matters in buffer names.

  Each buffer records individually what file it is visiting, whether
it is modified, and what major mode and minor modes are in effect in it.
Any Emacs variable can be made @dfn{local to} a particular buffer, meaning
its value in that buffer can be different from the value in other buffers.
@xref[Variables].

@section[Creating and Selecting Buffers]

@table 7
@item C-x b
Select or create a buffer.
@item C-x C-b
List the existing buffers.
@end table

@kindex{C-x b}@cfindex{switch-to-buffer}
  To select the buffer named @var{bufname}, type @kbd{C-x b @var[bufname]
@key(RET)}.  This is the command @code{switch-to-buffer} with argument
@var{bufname}.  Because completion is provided for buffer names, you can
abbreviate the buffer name (@pxref[Completion]).  An empty argument to @kbd{C-x b} specifies the
most recently selected buffer that is not displayed in any window.

@kindex{C-x C-b}@cfindex{list-buffers}
  To print a list of all the buffers that exist, type @kbd{C-x C-b}
(@code{list-buffers}).  Each buffer's name, major mode and visited file are
printed.  @samp{*} at the beginning of a line indicates the buffer is
``modified''.  If several buffers are modified, it may be time to save some
with @kbd{C-x s} (@pxref[Saving]).  @samp{%} indicates a read-only buffer.
@samp{.} marks the selected buffer.  Here is an example of a buffer list:
@example
 MR Buffer         Size  Mode           File
 -- ------         ----  ----           ----
.*  gmacs.tex      421336 Text          /u2/emacs/man/gmacs.tex
    *Help*         1287  Fundamental	
    files.el       23076 Emacs-Lisp     /u2/emacs/lisp/files.el
  % RMAIL          64042 RMAIL          /u/rms/RMAIL
    emacs.tex      383402 Text          /u2/emacs/man/emacs.tex
 *% man            747   Dired		
    net.emacs      343885 Fundamental   /u/rms/net.emacs
    fileio.c       27691 C              /u2/emacs/src/fileio.c
    NEWS           67340 Text           /u2/emacs/etc/NEWS
@end example
@nopara
Note that the buffer @code{*Help*} was made by a help request;
it is not visiting any file.  The buffer @code{man} was made by
Dired on the directory @code{/u2/emacs/man}.

  Most buffers are created by visiting files, or by Emacs commands that
want to display some text, but you can also create a
buffer explicitly by typing @kbd{C-x b @var[bufname] @key(RET)}.  This
makes a new, empty buffer which is not visiting any file, and
selects it for editing.  Such buffers are used for making notes to
yourself.  If you try to save one, you are asked for the file name to use.
The new buffer's major mode is determined by the value of
@code{default-major-mode} (@pxref[Major Modes].

  Note that @kbd{C-x C-f}, and any other command for visiting a file,
can also be used to switch buffers.  @xref[Files].

@section{Miscellaneous Buffer Operations}

@table 7
@item C-x C-q
Toggle read-only status of buffer.
@item M-x rename-buffer
Change the name of the current buffer.
@item M-x view-buffer
Scroll through a buffer.
@end table

@cindex{read-only buffer}
@kindex{C-x C-q}
@cfindex{toggle-read-only}
@vindex{buffer-read-only}
  A buffer can be @dfn{read-only}, which means that commands to change its
text are not allowed.  Normally, read only buffers are made by subsystems
such as Dired and Rmail that have special commands to operate on the text;
a read-only buffer is also made if you visit a file that is protected so
you cannot write it.  If you wish to make changes in a read-only buffer,
use the command @kbd{C-x C-q} (@code{toggle-read-only}).  It makes a
read-only buffer writable, and makes a writable buffer read-only.  This
works by setting the variable @code{buffer-read-only}, which has a local
value in each buffer and makes the buffer read-only if its value is
non-@code{nil}.

@cfindex{rename-buffer}
  @kbd{M-x rename-buffer} changes the name of the current buffer.  Specify
the new name as a minibuffer argument.  There is no default.  If you
specify a name that is in use for some other buffer, an error happens and
no renaming is done.

@cfindex{view-buffer}
  @kbd{M-x view-buffer} is much like @kbd{M-x view-file} (@pxref[Filadv])
except that it examines an already existing Emacs buffer.  View mode
provides commands for scrolling through the buffer conveniently but not
for changing it. When you exit View mode, the value of point that resulted
from your perusal remains in effect.

  The commands @kbd{C-x a} (@code{append-to-buffer}) and @kbd{M-x
insert-buffer} can be used to copy text from one buffer to another.
@xref[Accumulating Text].

@section[Killing Buffers]

  After you use Emacs for a while, you may accumulate a large number of
buffers.  You may then find it convenient to eliminate the ones you no
longer need.  There are several commands provided for doing this.

@c WideCommands
@table 7
@item C-x k
Kill a buffer, specified by name.
@item M-x kill-some-buffers
Offer to kill each buffer, one by one.
@end table

@cfindex{kill-buffer}
@cfindex{kill-some-buffers}
@kindex{C-x k}

  @kbd{C-x k} (@code{kill-buffer}) kills one buffer, whose name you specify
in the minibuffer.  The default, used if you type just @key(RET) in the
minibuffer, is to kill the current buffer.  If the current buffer is
killed, another buffer is selected; a buffer that has been selected recently
but does not appear in any window now is chosen to be selected.  If the buffer being
killed is modified (has unsaved editing) then you are asked to confirm with
`yes' before the buffer is killed.

  The command @kbd{M-x kill-some-buffers} asks about each buffer, one by
one.  An answer of @kbd{Y} means to kill the buffer.  Killing the current
buffer or a buffer containing unsaved changes selects a new buffer or
asks for confirmation just like @code{kill-buffer}.

@section{Operating on Several Buffers}
@cindex{buffer menu}

  The @dfn{buffer-menu} facility is like a ``Dired for buffers''; it allows
you to request operations on various Emacs buffers by editing an Emacs
buffer containing a list of them.

@table 7
@item M-x buffer-menu
Begin editing a buffer listing all Emacs buffers.
@end table

@cfindex{buffer-menu}
  The command @code{buffer-menu} writes a list of all Emacs buffers into
the buffer @code{*Buffer List*}, and selects that buffer in Buffer Menu
mode.  The buffer is read-only, and can only be changed through the special
commands described in this section.  Most of these commands are graphic
characters.  The usual Emacs cursor motion commands can be used in the
@code{*Buffer List*} buffer.  The following special commands apply to
the buffer described on the current line.

@table 7
@item k
Request to kill the buffer.  The request shows as a @samp{K} on the line,
before the buffer name.  Requested kills take place when the @kbd{x}
command is used.
@item s
Request to save the buffer.  The request shows as an @samp{S} on the line.
Requested saves take place when the @kbd{x} command is used.  You may
request both saving and killing for one buffer.
@item ~
Mark buffer ``unmodified''.  The command @kbd{~} does this, immediately
when typed.
@item x
Perform previously requested kills and saves.
@end table

  There are also special commands to use the buffer list to select another
buffer, and to specify one or more other buffers for display in additional windows.

@table 7
@item 1
Select the buffer in a full-screen window.  This command takes effect immediately.
@item 2
Use two windows, with this buffer in one, and the previously selected
buffer (aside from the buffer @code{*Buffer List*}) in the other.
@item q
Select this buffer, and also display in other windows any buffers
previously marked with the @kbd{m} command.  If there are no such buffers,
this command is equivalent to @kbd{1}.
@item m
Mark this buffer to be displayed in another window if the @kbd{q} command
is used.  The request shows as a @samp{>} at the beginning of the line.
The same buffer may not have both a kill request and a display request.
@end table

  All the commands that put in marks to request operations later also move
down a line, and accept a numeric argument as a repeat count.

  The command @kbd{u} cancels any request marked for the
current line, and moves down; @key(DEL) does so for the previous line,
and moves up to it.

  All that @code{buffer-menu} does directly is create and select a
suitable buffer, and turn on Buffer Menu mode.  Everything else described
above is implemented by the special commands provided in Buffer Menu mode.
One consequence of this is that you can switch from the @code{buffer-menu}
buffer to another Emacs buffer, and edit there.  You can reselect the
@code{buffer-menu} buffer later, to perform the operations already
requested, or you can kill it, or pay no further attention to it.

  The only difference between @code{buffer-menu} and @code{list-buffers} is
that @code{buffer-menu} selects the @code{*Buffer List*} buffer and
@code{list-buffers} does not.  If you run @code{list-buffers} (that is, type
@kbd{C-x C-b}) and select the buffer list manually, you can use all of the
commands described above.

@node[Windows,Major Modes,Buffers,Top]

@chapter[Multiple Windows]
@setref Windows
@cindex{windows}

  Emacs can slit the screen into two or many windows, which can display
parts of different buffers, or different parts of one buffer.

  When multiple windows are being displayed, each window has an Emacs buffer
designated for display in it.  The same buffer may appear in more than one
window; if it does, any changes in its text are displayed in all the
windows where it appears.  But the windows showing the same buffer can show
different parts of it, because each window has its own value of point.

@cindex{selected window}
  At any time, one of the windows is the @dfn{selected window}; the buffer
this window is displaying is the current buffer.  The terminal's cursor
shows the location of point in this window.  Each other window has a
location of point as well, but since the terminal has only one cursor there
is no way to show where those locations are.

  Commands to move point affect the value of point for the selected Emacs
window only.  They do not change the value of point in any other Emacs
window, even one showing the same buffer.  The same is true for commands
such as @kbd{C-x b} to change the selected buffer in the selected window;
they do not affect other windows at all.  However, there are other commands
such as @kbd{C-x 4 b} that select a different window and switch buffers in
it.  Also, all commands that display information in a window, including
(for example) @kbd{C-h f} (@code{describe-function}) and @kbd{C-x C-b}
(@code{list-buffers}), work by switching buffers in a nonselected window
without affecting the selected window.

  Each window has its own mode line, which displays the buffer name,
modification status and major and minor modes of the buffer that is
displayed in the window.  @xref[Mode Line], for full details on the mode line.

@c WideCommands
@table 7
@item C-x 2
Split the selected window into two windows, one above the other.
@item C-x 5
Split the selected window into two windows, side by side.
@item C-x o
Select another window (@kbd{o}, not zero).
@item C-x 0
Get rid of the selected window (and select some other window).
@item C-x 1
Get rid of all windows except the current one.
@item C-x 4
Prefix key for commands to select a buffer in various ways ``in another window''.
@item C-x ^
Make the selected window bigger, at the expense of the other(s).
@item C-M-v
Scroll the other window.
@end table

@kindex{C-x 2}
@cfindex{split-window-vertically}
  The command @kbd{C-x 2} (@code{split-window-vertically}) breaks the
selected window into two windows, one above the other.  Both windows start
out displaying the same buffer, with the same value of point.  By default
the two windows each get half the height of the window that was split; a
numeric argument specifies how many lines to give to the top window.

@kindex{C-x 5}
@cfindex{split-window-horizontally}
  @kbd{C-x 5} (@code{split-window-horizontally}) breaks the selected window
into two side-by-side windows.  A numeric argument specifies how many
columns to give the one on the left.  A line of vertical bars separates the
two windows.  Windows that are not the full width of the screen have mode
lines, but they are truncated; also, they do not appear in inverse video.
The Emacs display routines have not been told how to display a region of
inverse video that is only part of a line on the screen.

@vindex{truncate-partial-width-windows}
  When a window is less than the full width, text lines too long to fit are
frequent.  Continuing all those lines might be confusing.  The variable
@code{truncate-partial-width-windows} can be set non-@code{nil} to force
truncation in all windows less than the full width of the screen,
independent of the buffer being displayed and its value for
@code{truncate-lines}.  @xref[Continuation Lines].

  Horizontal scrolling is often used in side-by-side windows.
@xref[Display].

@kindex{C-x o}
@cfindex{other-window}
  To select a different window, use @kbd{C-x o} (@code{other-window}).  That
is an @kbd{o}, for `other', not a zero.  When there are more than two windows,
this command moves through all the windows in a cyclic order, generally top
to bottom and left to right.  From the rightmost and bottommost window, it goes
back to the one at the upper left corner.  A numeric argument means
to move several steps in the cyclic order of windows.  A negative argument
moves around the cycle in the opposite order.  When the minibuffer is
active, the minibuffer is the last window in the cycle; you can switch from
the minibuffer window to one of the other windows, and later switch back
and finish supplying the minibuffer argument that is requested.
@xref[Minibuffer Edit].

@kindex{C-M-v}
@cfindex{scroll-other-window}
  The usual scrolling commands (@pxref[Display]) apply to the selected
window only, but there is one command to scroll the next window.
@kbd{C-M-v} (@code{scroll-other-window}) scrolls the window that @kbd{C-x
o} would select.  The kind of scrolling done is the same as for @kbd{C-v}.

@kindex{C-x 0}
@cfindex{delete-window}
  To delete a window, type @kbd{C-x 0} (@code{delete-window}).  The space
it used to occupy is distributed among the other active windows (but not
the minibuffer window, even if that is active at the time).  Once a window
is deleted, everything about it is forgotten; there is no automatic way to
make another window showing the same contents.

@kindex{C-x 1}
@cfindex{delete-other-windows}
  @kbd{C-x 1} (@code{delete-other-windows}) is more powerful than @kbd{C-x 0};
it deletes all the windows except the selected one (and the minibuffer);
the selected window expands to use the whole screen except for the echo area.

@kindex{C-x ^}
@cfindex{enlarge-window}
@kindex[C-x }]
@cfindex{enlarge-window-horizontally}
@vindex{window-min-height}
@vindex{window-min-width}
  To readjust the division of space among existing windows, use  @kbd{C-x
^} (@code{enlarge-window}).  It makes the currently selected window get one
line bigger, or as many lines as is specified with a numeric argument.
With a negative argument, it makes the selected window smaller.  @kbd[C-x
}] (@code{enlarge-window-horizontally}) makes the selected window wider
by the specified number of columns.  The extra screen space given to a
window comes from one of its neighbors, if that is possible; otherwise, all
the competing windows are shrunk in the same proportion.  If this makes any
windows too small, those windows are deleted and their space is divided up.
The minimum size is specified by the variables @code{window-min-height} and
@code{window-min-width}.

@kindex{C-x 4}
@cfindex{Modified Two Windows}
  @kbd{C-x 4} is a prefix key for commands that select another window
(splitting the window if there is only one) and select a buffer in that
window.  Different @kbd{C-x 4} commands have different ways of finding the
buffer to select.

@table 7
@item C-x 4 b @var[bufname] @key(RET)
Select buffer @var[bufname] in another window.  This runs @code{pop-to-window}.
@item C-x 4 f @var[filename] @key(RET)
Visit file @var[filename] and select its buffer in another window.  This
runs @code{find-file-other-window}.
@item C-x 4 d @var[directory] @key(RET)
Select a Dired buffer for directory @var[directory] in another window.
This runs @code{dired-other-window}.
@item C-x 4 m
Start composing a mail message in another window.  This runs
@code{mail-other-window}, and its same-window version is @kbd{C-x m}.
@xref[Mail].
@item C-x 4 .
Find a tag in the current tag table in another window.  This runs
@code{find-tag-other-window}, the multiple-window variant of @kbd{M-.}.
@xref[Tags].
@end table

@node[Major Modes,Indentation,Windows,Top]

@chapter[Major Modes]
@setref Major Modes
@cindex{major modes}
@kindex{TAB}
@kindex{DEL}
@kindex{LFD}

  Emacs has many different @dfn[major modes], each of which customizes
Emacs for editing text of a particular sort.  The major modes are mutually
exclusive, and each buffer has one major mode at any time.  The mode line
normally contains the name of the current major mode, in parentheses.
@xref[Mode Line].

  The least specialized major mode is called @dfn[Fundamental mode].  This
mode has no mode-specific redefinitions or variable settings, so that each
Emacs command behaves in its most general manner, and each option is in its
default state.  For editing any specific type of text, such as Lisp code or
English text, you should switch to the appropriate major mode, such as Lisp
mode or Text mode.

  Selecting a major mode changes the meanings of a few keys to become
more specifically adapted to the language being edited.  The ones which
are changed frequently are @key(TAB),
@key(DEL), and @key(LFD).  In addition, the commands which handle
comments use the mode to determine how comments are to be delimited.  Many
major modes redefine the syntactical properties of characters appearing in
the buffer.  @xref[Syntax].

  The major modes fall into three major groups.  Lisp mode (which has
several variants), C mode and Muddle mode are for specific programming
languages.  Text mode is for editing English text.  The remaining major
modes are not intended for use on user's files; they are used in buffers
created for specific purposes by Emacs, such as Dired mode for buffers made
by Dired (@pxref[Dired]), and Mail mode for buffers made by @kbd{C-x m}
(@pxref[Mail]), and Shell mode for buffers used for communicating with an
inferior shell process (@pxref[Shell]).

  Selecting a new major mode is done with an @kbd{M-x} command.  From the
name of a major mode, add @code{-mode} to get the name of a command function
to select that mode.  Thus, you can enter Lisp mode by executing @kbd{M-x
lisp-mode}.

@vindex{auto-mode-alist}
  When you visit a file, Emacs usually chooses the right major mode based
on the file's name.  For example, files whose names end in @code{.c} are
edited in C mode.  The correspondence between file names and major mode is
controlled by the variable @code{auto-mode-alist}.  Its value is a list in
which each element has the form
@example
(@var[regexp] . @var[mode-function])
@end example
@nopara
For example, one element normally found in the list has the form
@code{(@ttfont["\\.c$"] . c-mode)}, and it is responsible for selecting C mode for
files whose names end in @code{.c}.

@cindex{Mode attribute}
  You can specify which major mode should be used for editing a certain
file by a special sort of text in the first nonblank line of the file.
The mode name should appear in this line both preceded and followed by
@samp{-*-}.  Other text may appear on the line as well.
For example,
@example
;-*-Lisp-*-
@end example
@nopara
tells Emacs to use Lisp mode.  Note how the semicolon is used to make Lisp
treat this line as a comment.  Such an explicit specification overrides any
defaulting based on the file name.

  Another format of mode specification is
@example
-*-Mode: @var[modename];-*-
@end example
@nopara
which allows other things besides the major mode name to be specified.
However, Emacs does not look for anything except the mode name.

@vindex{default-major-mode}
  When a file is visited that does not specify a major mode to use, or when
a new buffer is created with @kbd{C-x b}, the major mode used is that
specified by thef variable @code{default-major-mode}.  Normally this value
is the symbol @code{fundamental-mode}, which specifies Fundamental mode.  If
@code{default-major-mode} is @code{nil}, the major mode is taken from the
previously selected buffer.

  Most programming language major modes specify that only blank lines
separate paragraphs.  This is so that the paragraph commands remain useful.
@xref[Paragraphs].  They also cause Auto Fill mode to use the definition of
@key(TAB) to indent the new lines it creates.  This is because most lines
in a program are usually indented.  @xref[Indentation].

@menu
  Here are pointers to descriptions of the several major modes.

* Text::                Text mode is for editing English text.
* Lisp::                Lisp mode is for Lisp.
* MIDAS::               MIDAS mode is good for assembler code.
* PL1: (EPL1),          PL1 mode is the archetype from which modes
                        for many block structured languages are
                        defined.
* TEX: (ETEX),          @TeX mode.  @TeX is a text formatter.
* Bolio::
@end menu

@node[Indentation,Text,Major Modes,Top]

@chapter{Indentation}
@setref Indentation
@cindex{indentation}

@c WideCommands
@table 7
@item @key(TAB)
Indent current line ``appropriately'' in a mode-dependent fashion.
@item @key(LFD)
Perform @key(RET) followed by @key(TAB).
@item M-^
Merge two lines.  This would cancel out the effect of @key(LFD).
@item C-M-o
Split line at point; text on the line after point becomes a new line
indented to the same column that it now starts in.
@item M-m
Move (forward or back) to the first nonblank character on the current line.
@item C-M-\
Indent several lines to same column.
@item C-x @key(TAB)
Shift block of lines rigidly right or left.
@item M-x indent-relative
Indent to an indentation point in the previous line.
@end table

@kindex{TAB}@cindex{indentation}
  Most programming languages have some indentation convention.  For Lisp
code, lines are indented according to their nesting in parentheses.  The
same general idea is used for C code, though many details are different.

  Whatever the language, to indent a line, use the @key(TAB) command.  Each
major mode defines this command to perform the sort of indentation
appropriate for the particular language.  In Lisp mode, @key(TAB) aligns
the line according to its depth in parentheses.  No matter where in the
line you are when you type @key(TAB), it aligns the line as a whole.  In C
mode, @key(TAB) implements a subtle and sophisicated indentation style that
knows about many aspects of C syntax.

@kindex{TAB}
@kindex{LFD}
@cfindex{indent-new-line}
  In Text mode, @key(TAB) runs the command @code{tab-to-tab-stop}, which
indents to the next tab stop column.  You can set the tab stops with
@kbd{M-x edit-tab-stops}.

  If you just want to insert a tab character in the buffer, you can type
@kbd{C-q @key(TAB)}.

@cfindex{Auto Fill Mode}
@c ??? Explain what Emacs has instead of space-indent-flag.

@kindex{M-m}
@cfindex{back-to-indentation}
  To move over the indentation on a line, do @kbd{Meta-m}
(@code{back-to-indentation}).  This commands, given anywhere on a line,
positions point at the first nonblank character on the line.

  To insert an indented line before the current line, do @kbd{C-a C-o
@key(TAB)}.  To make an indented line after the current line, use @kbd{C-e
@key(LFD)}.

@kindex{C-M-o}
@cfindex{split-line}
  @kbd{C-M-o} (@code{split-line}) moves the text from point to the end of
the line vertically down, so that the current line becomes two lines.
@kbd{C-M-o} first moves point forward over any spaces and tabs.  Then it
inserts after point a newline and enough indentation to reach the same
column point is on.  Point remains before the inserted newline; in this
regard, @kbd{C-M-o} resembles @kbd{C-o}.

@kindex{M-Backslash}
@kindex{M-^}
@cfindex{delete-horizontal-space}
@cfindex{delete-indentation}
  To join two lines cleanly, use the @kbd{Meta-^}
(@code{delete-indentation}) command to delete the indentation at the front
of the current line, and the line boundary as well.  They are replaced by a
single space, or by no space if at the beginning of a line or before a
@samp{)} or after a @samp{(}.  To delete just the indentation of a
line, go to the beginning of the line and use @kbd{Meta-\}
(@code{delete-horizontal-space}), which deletes all spaces and tabs around
the cursor.

@kindex{C-M-Backslash}
@kindex{C-x TAB}
@cfindex{indent-region}
@cfindex{indent-rigidly}
  There are also commands for changing the indentation of several lines at
once.  @kbd{Control-Meta-\} (@code{indent-region}) gives each line which
begins in the region the ``usual'' indentation by invoking @key(TAB) at the
beginning of the line.  A numeric argument specifies the indentation, and
each line is shifted left or right so that it has exactly that much.
@kbd{C-x @key(TAB)} (@code{indent-rigidly}) moves all of the lines in the
region right by its argument (left, for negative arguments).  The whole
group of lines move rigidly sideways, which is how the command gets its
name.

@cfindex{indent-relative}
  @kbd{M-x indent-relative} indents at point based on the previous line
(actually, the previous nonempty line.)  It inserts whitespace at point,
moving point, until it is underneath an indentation point in the previous
line.  An indentation point is the end of a sequence of whitespace or the
end of the line.  If point is farther right than any indentation point in
the previous line, the whitespace before point is deleted and the first
indentation point then applicable is used.  If no indentation point is
applicable even then, @code{tab-to-tab-stop} is run.

  @code{indent-relative} is the definition of @key(TAB) in Indented Text
mode.  @xref[Text].

@section[Tab Stops]
@setref Tab Stops

@kindex{M-i}
@cfindex{tab-to-tab-stop}
  For typing in tables, you can use Text mode's definition of @key(TAB),
@code{tab-to-tab-stop}.  This command inserts indentation before point,
enough to reach the next tab stop column.  If you are not in Text mode,
this function can be found on @kbd{M-i} anyway.

@cfindex{edit-tab-stops}
@cfindex{edit-tab-stops-note-changes}
@kindex{C-x C-s}
@vindex{tab-stop-list}
  The tab stops used by @kbd{M-i} can be set arbitrarily by the user.
They are stored in a variable called @code{tab-stop-list}, as a list of
column-numbers in increasing order.

  The convenient way to set the tab stops using @kbd{M-x edit-tab-stops},
which creates and selects a buffer containing a description of the tab stop
settings.  You can edit this buffer to specify different tab stops, and
then type @kbd{C-x C-s} to make those new tab stops take effect.  In the
tab stop buffer, @kbd{C-x C-s} runs the function
@code{edit-tab-stops-note-changes} rather than its usual definition
@code{save-buffer}.  @code{edit-tab-stops} records which buffer was current
when you invoked it, and stores the tab stops back in that buffer; normally
all buffers share the same tab stops and changing them in one buffer
affects all, but if you happen to make @code{tab-stop-list} local in one
buffer then @code{edit-tab-stops} will edit the tab stops that are
effective in the buffer that you invoke it for.

  Here is what the text representing the tab stops looks like for ordinary
tab stops every eight columns.

@example
        :       :       :       :       :       :       :       :       :
0         1         2         3         4         5         6         7
0123456789012345678901234567890123456789012345678901234567890123456789012
To install changes, type C-X C-S
@end example

  The first line contains a colon at each tab stop.  The remaining lines
are present just to help you see where the colons are and know what to do.

  Note that the tab stops that control @code{tab-to-tab-stop} have nothing
to do with displaying tab characters in the buffer.  @xref[Characters],
for more information on that.

@section[Tabs vs. Spaces]

@vindex{indent-tabs-mode}
  Emacs normally uses both tabs and spaces to indent lines.  If you prefer,
all indentation can be made from spaces only.  To request this, set
@code{indent-tabs-mode} to @code{nil}.

@cfindex{tabify}
@cfindex{untabify}
  There are also commands to convert tabs to spaces or vice versa,
always preserving the columns of all nonblank text.  @kbd{M-x tabify}
scans the region for sequences of spaces, and converts sequences of at
least three spaces to tabs if that can be done without changing
indentation.  @kbd{M-x untabify} changes all tabs in the region to
appropriate numbers of spaces.

@node[Text,,Indentation,Top]

@chapter[Commands for Human Languages]
@setref Text
@cindex{text}

  The term @dfn{text} has two widespread meanings in our area
of the computer field.  One is, data that is a sequence of characters.
Any file that you edit with Emacs is text, in this sense of the word.
The other meaning is more restrictive; it is, a sequence of characters
in a human language for humans to read (possibly after processing by
a text formatter), as opposed to a program or commands for a program.

  Human languages have syntactic/stylistic conventions that can be
supported or used to advantage by editor commands: conventions
involving words, sentences, paragraphs, and capital letters.  This
chapter describes Emacs commands for all of these things.  In addition,
there is a major mode, Text mode, which customizes Emacs in small ways
for editing a file of human language text.  There are also commands
for @dfn{filling}, or rearranging paragraphs into lines of approximately
equal length.

  The commands for moving over and killing words (@pxref[Words]),
sentences (@pxref[Sentences]) and paragraphs (@pxref[Paragraphs]) are
are primarily intended for human-language text, but are very often useful
in editing programs also.

@section{Text Mode}

@cfindex{tab-to-tab-stop}
@cfindex{edit-tab-stops}
@cindex{Text mode}
@kindex{TAB}
@cfindex{text-mode}
  Editing files of text in a human language ought to be done using Text
mode rather than Lisp or Fundamental mode.  Invoke @kbd{M-x text-mode} to
enter Text mode.  In Text mode, @key{TAB} runs the function
@code{tab-to-tab-stop}, which allows you to use arbitrary tab stops set
with @kbd{M-x edit-tab-stops} (@pxref[Tab Stops]).  Features concerned with
comments in programs are turned off except when explicitly invoked.  The
syntax table is changed so that periods are not considered part of a word,
while apostrophes, backspaces and underlines are.

@cfindex{indented-text-mode}
  A similar variant mode is Indented Text mode, intended for editing text
in which most lines are indented.  This mode defines @key(TAB) to run
@code{indent-relative} (@pxref[Indentation]), and makes Auto Fill indent
the lines it creates.  The result is that normally a line made by Auto
Filling, or by @key(LFD), is indented just like the previous line.  Use
@kbd{M-x indented-text-mode} to select this mode.

@menu
* Words::           moving over and killing words.
* Sentences::       moving over and killing sentences.
* Paragraphs::	    moving over paragraphs.
* Pages::	    moving over pages.
* Filling::         filling or justifying text
* Case::            changing the case of text
@end menu

@node[Words,Sentences,Text,Text]

@section[Words]
@setref Words
@cindex{words}@cindex{Meta}

  Emacs has commands for moving over or operating on words.  By
convention, the keys for them are all @kbd{Meta-} characters.

@c widecommands
@table 7
@item M-f
Move Forward over a word.
@item M-b
Move Backward over a word.
@item M-d
Kill up to the end of a word.
@item M-@key(DEL)
Kill back to the beginning of a word.
@item M-@@
Mark the end of the next word.
@item M-t
Transpose two words;  drag a word forward
or backward across other words.
@end table

  Notice how these keys form a series that parallels the
character-based @kbd{C-f}, @kbd{C-b}, @kbd{C-d}, @kbd{C-t} and
@key(DEL).  @kbd{M-@@} is related to @kbd{C-@@}, which is an alias for
@kbd{C-@key(SPC)}.

@kindex{M-f}
@kindex{M-b}
@cfindex{forward-word}
@cfindex{backward-word}
  The commands @kbd{Meta-f} (@code{forward-word}) and @kbd{Meta-b}
(@code{backward-word}) move forward and backward over words.  They are thus
analogous to @kbd{Control-f} and @kbd{Control-b}, which move over single
characters.  Like their @kbd{Control-} analogues, @kbd{Meta-f} and
@kbd{Meta-b} move several words if given an argument.  @kbd{Meta-f} with a
negative argument moves backward, and @kbd{Meta-b} with a negative argument
moves forward.  Forward motion stops right after the last letter of the
word, while backward motion stops right before the first letter.

@kindex{M-d}
@cfindex{kill-word}
  @kbd{Meta-d} (@code{kill-word}) kills the word after point.  To be
precise, it kills everything from point to the place @kbd{Meta-f} would
move to.  Thus, if point is in the middle of a word, @kbd{Meta-d} kills
just the part after point.  If some punctuation comes between point and the
next word, it is killed along with the word.  If you wish to kill only the
next word but not the punctuation before it, simply do @kbd{Meta-f} to get
the end, and kill the word backwards with @kbd{Meta-@key(DEL)}.
@kbd{Meta-d} takes arguments just like @kbd{Meta-f}.

@cfindex{backward-kill-word}
@kindex{M-DEL}
  @kbd{Meta-@key(DEL)} (@code{backward-kill-word}) kills the word before
point.  It kills everything from point back to where @kbd{Meta-b} would
move to.  If point is after the space in @w{@samp[FOO, BAR]}, then
@w{@samp[FOO, ]} is killed.  If you wish to kill just @samp{FOO}, do
@kbd{Meta-b Meta-d} instead of @kbd{Meta-@key(DEL)}.

@cindex{transposition}
@kindex{M-t}
@cfindex{transpose-words}
  @kbd{Meta-t} (@code{transpose-words}) exchanges the words before or
containing point with the following word.  The delimiter
characters between the words do not move.  For example, @w{@samp[FOO, BAR]}
transposes into @w{@samp[BAR, FOO]} rather than @samp{@w[BAR FOO,]}.
@xref[Transposition], for more on transposition and on arguments to
transposition commands.

@kindex{M-@@}@cfindex{mark-word}
  To operate on the next @var{n} words with an operation which applies
between point and mark, you can either set the mark at point and then move
over the words, or you can use the command @kbd{Meta-@@} (@code{mark-word})
which does not move point, but sets the mark where @kbd{Meta-f} would move
to.  It can be given arguments just like @kbd{Meta-f}.

@cindex{syntax table}
  The word commands' understanding of syntax is completely controlled by
the syntax table.  Any character can, for example, be declared to be a word
delimiter.  @xref[Syntax].

@node[Sentences,Paragraphs,Words,Text]

@section[Sentences]
@setref Sentences
@cindex{sentences}

  The Emacs commands for manipulating sentences and paragraphs are mostly
on @kbd{Meta-} keys, so as to be like the word-handling commands.

@table 7
@item M-a
Move back to the beginning of the sentence.
@item M-e
Move forward to the end of the sentence.
@item M-k
Kill forward to the end of the sentence.
@item C-x @key(DEL)
Kill back to the beginning of the sentence.
@end table

@kindex{M-a}
@kindex{M-e}
@cfindex{backward-sentence}
@cfindex{forward-sentence}
  The commands @kbd{Meta-a} and @kbd{Meta-e} (@code{backward-sentence} and
@code{forward-sentence}) move to the beginning and end of the current
sentence, respectively.  They were chosen to resemble @kbd{Control-a} and
@kbd{Control-e}, which move to the beginning and end of a line.  Unlike
them, @kbd{Meta-a} and @kbd{Meta-e} if repeated or given numeric arguments
move over successive sentences.  Emacs considers a sentence to end wherever
there is a @samp{.}, @samp{?} or @samp{!} followed by the end of a line or
two spaces, with any number of @samp{)}, @samp{]}, @samp{'}, or @samp{"}
characters allowed in between.  A sentence also begins or ends wherever a
paragraph begins or ends.

  Neither @kbd{M-a} nor @kbd{M-e} moves past the newline or spaces beyond
the sentence edge at which it is stopping.

@kindex{M-k}@kindex{C-x DEL}
@cfindex{kill-sentence}
@cfindex{backward-kill-sentence}
  Just as @kbd{C-a} and @kbd{C-e} have a kill command, @kbd{C-k}, to go
with them, so @kbd{M-a} and @kbd{M-e} have a corresponding kill command
@kbd{M-k} (@code{kill-sentence}) which kills from point to the end of the
sentence.  With minus one as an argument it kills back to the beginning of
the sentence.  Larger arguments serve as a repeat count.

  There is a special command, @kbd{C-x @key(DEL)}
(@code{backward-kill-sentence}) for killing back to the beginning of a
sentence, because this is useful when you change your mind in the middle of
composing text.

@vindex{sentence-end}
  The variable @code{sentence-end} controls recognition of the end of a
sentence.  It is a regexp that matches the last few characters of a
sentence, together with the whitespace following the sentence.  Its
normal value is
@example
"[.?!][]\")]*\\($\\|\t\\|  \\)[ \t\n]*"
@end example

@node[Paragraphs,Pages,Sentences,Text]

@section[Paragraphs]
@setref Paragraphs
@cindex{paragraphs}
@kindex{M-[}
@kindex{M-]}
@cfindex{backward-paragraph}
@cfindex{forward-paragraph}

  The Emacs commands for manipulating paragraphs are also @kbd{Meta-}
keys.

@table 7
@item M-[
Move back to previous paragraph beginning.
@item M-]
Move forward to next paragraph end.
@item M-h
Put point and mark around this or next paragraph.
@end table

  @kbd{Meta-[} (@code{backward-paragraph}) moves to the beginning of the
current or previous paragraph, while @kbd{Meta-]}
(@code{forward-paragraph}) moves to the end of the current or next
paragraph.  Blank lines and text formatter command lines separate
paragraphs and are not part of any paragraph.  Also, an indented line
starts a new paragraph.@refill

  In major modes for programs (as opposed to Text mode), paragraphs begin
and end only at blank lines.  This makes the paragraph commands continue to
be useful even though there are no paragraphs per se.

  When there is a fill prefix, then paragraphs are delimited by all lines
which don't start with the fill prefix.  @xref[Filling].

@kindex{M-h}
@cfindex{mark-paragraph}
  When you wish to operate on a paragraph, you can use the command
@kbd{Meta-h} (@code{mark-paragraph}) to set the region around it.  This
command puts point at the beginning and mark at the end of the paragraph
point was in.  If point is between paragraphs (in a run of blank lines, or
at a boundary), the paragraph following point is surrounded by point and
mark.  If there are blank lines preceding the first line of the paragraph,
one of these blank lines is included in the region.

  Thus, for example, @kbd{M-h C-w} kills the paragraph around or after
point.

@vindex{paragraph-start}
@vindex{paragraph-separate}
  The precise definition of a paragraph boundary is controlled by the
variables @code{paragraph-separate} and @code{paragraph-start}.  The value
of @code{paragraph-start} is a regexp that should match any line that
either starts or separates paragraphs.  The value of
@code{paragraph-separate} is another regexp that should match only lines
that separate paragraphs without being part of any paragraph.  For example,
normally @code{paragraph-start} is @code{"^[ \t\n\f]"} and
@code{paragraph-separate} is @code{"^[ \t\f]*$"}.

  Normally it is desirable for page boundaries to separate paragraphs.
The default values of these variables recognize the usual separator for
pages.

@node[Pages,Filling,Paragraphs,Text]

@section[Pages]
@setref Pages

@cindex{pages}
@cindex{formfeed}
  Files are often thought of as divided into @dfn[pages] by the
@dfn{formfeed} character (@ctl[L], octal code 014).  For example, if a
file is printed on a line printer, each page of the file, in this sense,
will start on a new page of paper.  Emacs treats a page-separator
character just like any other character.  It can be inserted with @kbd{C-q
C-l}, or deleted with @key(DEL).  Thus, you are free to paginate your
file, or not.  However, since pages are often meaningful divisions of the
file, commands are provided to move over them and operate on them.

@c WideCommands
@table 7
@item C-x C-p
Put point and mark around this page (or another page).
@item C-x [
Move point to previous page boundary.
@item C-x ]
Move point to next page boundary.
@item C-x l
Count the lines in this page.
@end table

@kindex{C-x [}
@kindex{C-x ]}
@cfindex{forward-page}
@cfindex{backward-page}
  The @kbd{C-x [} (@code{backward-page}) command moves point to
immediately after the previous page delimiter.  If point is already
right after a page delimiter, it skips that one and stops at the previous
one.  A numeric argument serves as a repeat count.  The @kbd{C-x ]}
(@code{forward-page}) command moves forward past the next page delimiter.

@kindex{C-x C-p}
@cfindex{mark-page}
  The @kbd{C-x C-p} command (@code{mark-page}) puts point at the beginning
of the current page and the mark at the end.  The page delimiter at the end
is included (the mark follows it).  The page delimiter at the front is
excluded (point follows it).  This command can be followed by @kbd{C-w} to
kill a page which is to be moved elsewhere.  If it is inserted after a page
delimiter, at a place where @kbd{C-x ]} or @kbd{C-x [} would take you,
then the page will be properly delimited before and after once again.

  A numeric argument to @kbd{C-x C-p} is used to specify which page to go
to, relative to the current one.  Zero means the current page.  One means
the next page, and -1 means the previous one.

@kindex{C-x l}
@cfindex{count-lines-page}
  The @kbd{C-x l} command (@code{count-lines-page}) is good for deciding
where to break a page in two.  It prints in the echo area the total number
of lines in the current page, and then divides it up into those preceding
the current line and those following, as in
@example
Page has 96 (72+25) lines
@end example
@nopara
  Notice that the sum is off by one; this is correct if point is not at the
front of a line.

@vindex{page-delimiter}
  The variable @code{page-delimiter} should have as its value a regexp that
matches the beginning of a line that separates pages.  This is what defines
where pages begin.  The normal value of this variable is @code{"^\f"},
which matches a formfeed character at the beginning of a line.

@node[Filling,Case,Pages,Text]

@section[Filling Text]
@setref Filling
@cindex{filling}

@cindex{Auto Fill mode}
  With Auto Fill mode, text can be @dfn[filled] (broken up into lines that
fit in a specified width) as you insert it.  If you alter existing text it
may no longer be properly filled; then explicit commands for filling can be
used.

@table 7
@item M-x auto-fill-mode
Enable or disable Auto Fill mode.
@item @key(SPC)
@itemx @key(RET)
In Auto Fill mode, break lines when appropriate.
@item M-q
Fill current paragraph.
@item M-g
Fill each paragraph in the region.
@item M-x fill-region-as-paragraph.
Fill the region, considering it as one paragraph.
@item M-s
Center a line.
@end table

@cfindex{auto-fill-mode}
  @kbd{M-x auto-fill-mode} turns Auto Fill mode on if it was off, or off
if it was on.  With a positive numeric argument it always turns Auto Fill
mode on, and with a negative argument always turns it off.  You can
see when Auto Fill mode is in effect by the presence of the word
@samp{Fill} in the mode line, inside the parentheses.  Auto Fill mode is
a minor mode, turned on or off for each buffer individually.
@xref[Minor Modes].

  In Auto Fill mode, lines are broken automatically at spaces when they get
longer than the desired width.  Line breaking and rearrangement takes place
only when you type @key(SPC) or @key(RET).  If you wish to insert a
space or newline without permitting line-breaking, type @kbd{C-q
@key(SPC)} or @kbd{C-q @key(LFD)} (recall that a newline is really
a linefeed).  Also, @kbd{C-o} inserts a newline without line
breaking.

  Auto Fill mode works well with Lisp mode, because when it makes a new
line in Lisp mode it indents that line with @key(TAB).  If a line ending
in a comment gets too long, the text of the comment is split into two
comments.

@kindex{M-q}
@cfindex{fill-paragraph}
  Auto Fill mode does not refill entire paragraphs.  It can break lines but
cannot merge lines.  So editing in the middle of a paragraph can result in
a paragraph that is not correctly filled.  To refill a paragraph, use the
command @kbd{Meta-q} (@code{fill-paragraph}).  It causes the paragraph that
point is inside, or the one after point if point is between paragraphs, to
be refilled.  All the line-breaks are removed, and then new ones are
inserted where necessary.  @kbd{M-q} can be undone with @kbd{C-_}.
@xref[Undo].

@kindex{M-g}
@cfindex{fill-region}
  To refill many paragraphs, use @kbd{M-g} (@code{fill-region}), which
divides the region into paragraphs and fills each of them.

@cfindex{fill-region-as-paragraph}
  @kbd{Meta-q} and @kbd{Meta-g} use the same criteria as @kbd{Meta-h} for
finding paragraph boundaries (@pxref[Paragraphs]).  For more control, you
can use @kbd{M-x fill-region-as-paragraph}, which refills everything
between point and mark.  This command recognizes only blank lines as
paragraph separators.

@cindex{justification}
  A numeric argument to @kbd{M-g} or @kbd{M-q} causes it to @dfn{justify}
the text as well as filling it.  This means that extra spaces are inserted
to make the right margin line up exactly at the fill column.  Extra spaces
are removed by @kbd{M-q} or @kbd{M-g} with no argument.

@kindex{M-s}
@cindex{centering}
@cfindex{center-line}
  The command @kbd{Meta-s} (@code{center-line}) centers the current line
within the current fill column.  With an argument, it centers several lines
individually and moves past them.

@vindex{fill-column}
@vindex{default-fill-column}
  The maximum line width for filling is in the variable
@code{fill-column}.  This variable has a separate value in each buffer;
setting it in one buffer has no effect on any other buffer.  The initial
value in a new buffer is taken from the variable
@code{default-fill-column}.

@kindex{C-x f}
@cfindex{set-fill-column}
  The easiest way to set @code{fill-column} is to use the command @kbd{C-x
f} (@code{set-fill-column}).  With no argument, it sets @code{fill-column}
to the current horizontal position of point.  With a numeric argument, it
uses that as the new fill column.

@cindex{fill prefix}
@kindex{C-x .}
@cfindex{set-fill-prefix}
@vindex{fill-prefix}
  To fill a paragraph in which each line starts with a special marker
(which might be a few spaces, giving an indented paragraph), use the
@dfn[fill prefix] feature.  The fill prefix is a string which Emacs expects
every line to start with, and which is not included in filling.  It is
stored in the variable @code{fill-prefix}.

  To specify a fill prefix, move to a line that starts with the desired
prefix, put point at the end of the prefix, and give the command
@w{@kbd[C-X .]} (@code{set-fill-prefix}).  That's a period after the
@kbd{C-x}.  To turn off the fill prefix, specify an empty prefix: type
@w[@kbd{C-x .}] with point at the beginning of a line.

  When a fill prefix is in effect, the fill commands remove the fill prefix
from each line before filling and insert it on each line after filling.  In
Auto Fill mode, @key(SPC) also inserts the fill prefix on any new line.
Lines that do not start with the fill prefix are considered to start
paragraphs, both in @kbd{M-q} and the paragraph commands; this is just
right if you are using paragraphs with hanging indentation (every line
indented except the first one).  Lines which are blank or indented once the
prefix is removed also separate or start paragraphs; this is what you want
if you are writing multi-paragraph comments with a comment delimiter on
each line.

  Many users like Auto Fill mode and want to use it in all text files.
Execute the following Lisp expression, perhaps in your init file, to
cause Auto Fill mode to be turned on whenever Text mode is entered:
@lisp
(setq text-mode-hook 'turn-on-auto-fill)
@end lisp

@node[Case,,Filling,Text]

@section[Case Conversion Commands]
@setref Case
@cindex{case conversion}

  Emacs has commands for converting either a single word or any arbitrary
range of text to upper case or to lower case.

@c WideCommands
@table 7
@item M-l
Convert following word to lower case.
@item M-u
Convert following word to upper case.
@item M-c
Capitalize the following word.
@item C-x C-l
Convert region to lower case.
@item C-x C-u
Convert region to upper case.
@end table

@kindex{M-l}
@kindex{M-u}
@kindex{M-c}
@cindex{words}
@cfindex{downcase-word}
@cfindex{upcase-word}
@cfindex{capitalize-word}
  The word conversion commands are the most useful.  @kbd{Meta-l}
(@code{downcase-word}) converts the word after point to lower case, moving
past it.  Thus, repeating @kbd{Meta-l} converts successive words.
@kbd{Meta-u} (@code{upcase-word}) converts to all capitals instead,
while @kbd{Meta-c} (@code{capitalize-word}) puts the first letter of the
word into upper case and the rest into lower case.  All these commands
convert several words at once if given an argument.  They are especially
convenient for converting a large amount of text from all upper case to
mixed case, because you can move through the text using @kbd{M-l},
@kbd{M-u} or @kbd{M-c} on each word as appropriate, occasionally using
@kbd{M-f} instead to skip a word.

  When given a negative argument, the word case conversion commands apply
to the appropriate number of words before point, but do not move point.
This is convenient when you have just typed a word in the wrong case.  You
can give the case conversion command and continue typing.

  If a word case conversion command is given in the middle of a word, it
applies only to the part of the word which follows point.  This is just
like what @kbd{Meta-d} (@code{kill-word}) does.  With a negative argument,
case conversion applies only to the part of the word before point.

@kindex{C-x C-l}
@kindex{C-x C-u}
@cindex{region}
@cfindex{downcase-region}
@cfindex{upcase-region}
  The other basic case conversion commands are @kbd{C-x C-u}
(@code{upcase-region}) and @kbd{C-x C-l} (@code{downcase-region}),
which convert everything between point and mark to the specified case.
Point and mark do not move.

@chapter[Editing Programs]
@setref Programs
@cindex{Lisp}
@cindex{C}

  Emacs has many commands designed to understand the syntax of programming
languages such as Lisp and C.  These commands can
@itemize @bullet
@item
Move over or kill balanced expressions or sexps (@pxref[Lists]).
@item
Move over or mark top-level balanced expressions (@dfn{defuns}, in Lisp;
functions, in C).
@item
Show how parentheses balance (@pxref[Matching]).
@item
Insert, kill or align comments (@pxref[Comments]).
@item
Follow the usual indentation conventions of the language
(@pxref[Grinding]).
@end itemize

  The commands for words, sentences and paragraphs are very useful in
editing code even though their canonical application is for editing human
language text.  Most symbols contain words; sentences can be found in
strings and comments.  Paragraphs per se are not present in code, but the
paragraph commands are useful anyway, because Lisp mode and C mode define
paragraphs to begin and end at blank lines.  Judicious use of blank lines
to make the program clearer will also provide interesting chunks of text
for the paragraph commands to work on.  @xref[Text].

@node[Program Modes, Lists, Comments, Programs]

@section[Major Modes for Programming Languages]
@setref Program Modes

@cindex{Lisp mode}
@cindex{C mode}
@cindex{Scheme mode}
  Emacs also has major modes for the programming languages Lisp, Scheme (a
variant of Lisp), C and Muddle.  Ideally, a major mode should be
implemented for each programming language that you might want to edit with
Emacs; but often the mode for one language can serve for other
syntactically similar languages.  The language modes that exist are those
that someone decided to trouble to write.

  There are several forms of Lisp mode, which differ in the way they
interface to Lisp execution.  @xref[Lisp Modes].

  Each of the programming language modes defines the @key(TAB) key to run
an indentation function that knows the indentation conventions of that
language and updates the current line's indentation accordingly.  For
example, in C mode @key(TAB) is bound to @code{c-indent-line}.
@key(LFD) is normally defined to do @key(RET) followed by
@key(TAB); thus, it too indents in a mode-specific fashion.

@kindex{DEL}
@cfindex{backward-delete-char-untabify}
  In most programming languages, indentation is likely to vary from line to
line.  So the major modes for those languages rebind @key(DEL) to
treat a tab as if it were the equivalent number of spaces (using the
command @code{backward-delete-char-untabify}).  This makes it possible to
rub out indentation one column at a time without worrying whether it is
made up of spaces or tabs.  Use @kbd{C-b C-d} to delete a tab character
before point, in these modes.

  Paragraphs are defined to start only with blank lines so that the
paragraph commands can be useful.  Auto Fill mode, if enabled in a
programming language major mode,
indents the new lines which it creates.

@node[Lists, Defuns, Lisp, Programs]

@section[Lists and Sexps]
@setref Lists

@c doublewidecommands
@table 7
@item C-M-f
Move forward over a sexp.
@item C-M-b
Move Backward over a sexp.
@item C-M-k
Kill sexp forward.
@item C-M-u
Move up and backward in list structure.
@item C-M-d
Move down and forward in list structure.
@item C-M-n
Move forward over a list (parenthetical grouping).
@item C-M-P
Move backward over a list (parenthetical grouping).
@item C-M-t
Transpose expressions.
@item C-M-@@
Put mark after following expression.
@end table

@cindex{Control-Meta}
  By convention, Emacs keys for dealing with balanced expressions are
usually @kbd{Control-Meta-} characters.  They tend to be analogous in
function to their @kbd{Control-} and @kbd{Meta-} equivalents.  These
commands are usually thought of as pertaining to expressions in programming
languages, but can be useful with any language in which some sort of
parentheses exist (including English).

@cindex{list}
@cindex{sexp}
@cindex{expression}
  These commands fall into two classes.  Some deal only with @dfn{lists}
(parenthetical groupings).  They see nothing except parentheses, brackets,
braces, and escape characters that might be used to quote those.  The other
commands deal with expressions or @dfn{sexps} (short for
@dfn{s-expression}, the ancient term for a balanced expression in Lisp).
A parenthetical grouping is one kind of sexp, but a symbol name is also
a sexp, and so is a string.  Numbers and character constants can also be
sexps.  The idea is to define the major mode for a language so that the
expressions of that language count as sexps, as much as possible.

  Except in Lisp-like languages, not all expressions can be sexps.  For
example, C mode does not recognize @code{foo + bar} as a sexp, even
though it @i[is] a C expression; it recognizes @code{foo} as one sexp
and @code{bar} as another, with the @code{+} as punctuation between them.
This is a fundamental ambiguity: both @code{foo + bar} and @code{foo} are
legitimate choices for the sexp to move over if point is at the
@code{f}.  Note that @code{(foo + bar)} is a sexp in C mode.

  Some languages have obscure forms of syntax for expressions that nobody
has bothered to make Emacs understand properly.

@kindex{C-M-f}
@kindex{C-M-b}
@cfindex{forward-sexp}
@cfindex{backward-sexp}
  To move forward over a sexp, use @kbd{C-M-f} (@code{forward-sexp}).  If
the first significant character after point is an opening delimiter
(@samp{(} in Lisp, @samp{(}, @samp{[} or @samp[{] in C),
@kbd{C-M-f} moves past the matching closing delimiter.  If the character
begins a symbol, string, or number, @kbd{C-M-f} moves over that.  If the
character after point is a closing delimiter, @kbd{C-M-f} just moves past
it.  (This last is not really moving across an sexp; it is an exception
which is included in the definition of @kbd{C-M-f} because it is as useful
a behavior as anyone can think of for that situation.)

  The command @kbd{C-M-b} (@code{backward-sexp}) moves backward over a
sexp.  The detailed rules are like those above for @kbd{C-M-f}, but with
directions reversed.  If there are any prefix characters (singlequote,
backquote and comma, in Lisp) preceding the sexp, @kbd{C-M-b} moves back
over them as well.

  @kbd{C-M-f} or @kbd{C-M-b} with an argument repeats that operation the
specified number of times; with a negative argument, it moves in the
opposite direction.

  The sexp commands move across comments as if they were whitespace, in
languages such as C where the comment-terminator can be recognized.  In
Lisp, and other languages where comments run until the end of a line, it is
very difficult to ignore comments when parsing backwards; therefore, the
sexp commands in such languages treat the text of comments as if it were
code.

@kindex{C-M-k}
@cfindex{kill-sexp}
  Killing a sexp at a time can be done with @kbd{C-M-k} (@code{kill-sexp}).
@kbd{C-M-k} kills the characters that @kbd{C-M-f} would move over.

@kindex{C-M-n}
@kindex{C-M-p}
@cfindex{forward-list}
@cfindex{backward-list}
  The @dfn{list commands} move over lists like the sexp commands but skip
blithely over any number of other kinds of sexps (symbols, strings, etc).
They are @kbd{C-M-n} (@code{forward-list}) and @kbd{C-M-p}
(@code{backward-list}).  The main reason they are useful is that they
usually ignore comments (since the comments usually do not contain any
lists).

@kindex{C-M-u}
@kindex{C-M-d}
@cfindex{backward-up-list}
@cfindex{down-list}
  @kbd{C-M-n} and @kbd{C-M-p} stay at the same level in parentheses, when
that's possible.  To move @i[up] one (or @var{n}) levels, use @kbd{C-M-u}
(@code{backward-up-list}).
@kbd{C-M-u} moves backward up past one unmatched opening delimiter.  A
positive argument serves as a repeat count; a negative argument reverses
direction of motion and also requests repetition, so it moves forward and
up a level.

  To move @i[down] in list structure, use @kbd{C-M-d} (@code{down-list}).
In Lisp mode, where @samp{(} is the only opening delimiter, this is nearly
the same as searching for a @samp{(}.

@cindex{transposition}
@kindex{C-M-t}
@cfindex{transpose-sexps}
  A somewhat random-sounding command which is nevertheless easy to use is
@kbd{C-M-t} (@code{transpose-sexps}), which drags the previous sexp
across the next one.  An argument serves as a repeat count, and a negative
argument drags backwards (thus canceling out the effect of @kbd{C-M-t} with
a positive argument).  An argument of zero, rather than doing nothing,
transposes the sexps at the point and the mark. 

@kindex{C-M-@@}
@cfindex{mark-sexp}
  To make the region be the next sexp in the buffer, use @kbd{C-M-@@}
(@code{mark-sexp}) which sets mark at the same place that @kbd{C-M-f} would
move to.  @kbd{C-M-@@} takes arguments like @kbd{C-M-f}.  In particular, a
negative argument is useful for putting the mark at the beginning of the
previous sexp.

  The list and sexp commands' understanding of syntax is completely
controlled by the syntax table.  Any character can, for example, be
declared to be an opening delimiter and act like an open parenthesis.
@xref[Syntax].

@node[Defuns,Lisp Search,Lists,Programs]

@section[Defuns]
@setref Defuns
@cindex{defuns}

  In Emacs, a list at the top level in the buffer is called a @dfn[defun].
The name derives from the fact that most top level lists in a Lisp file are
instances of the special form @code{defun}, but any top level list counts
as a defun in Emacs parlance regardless of what its contents are, and
regardless of the programming language in use.  For example, in C, the body
of a function definition is a defun.

@c doublewidecommands
@table 7
@item C-M-a
Move to beginning of defun.
@item C-M-e
Move to end of defun.
@item C-M-h
Put region around whole defun.
@end table

@kindex{C-M-a}
@kindex{C-M-e}
@kindex{C-M-h}
@cfindex{beginning-of-defun}
@cfindex{end-of-defun}
@cfindex{mark-defun}
  The commands to move to the beginning and end of the current defun are
@kbd{C-M-a} (@code{beginning-of-defun}) and @kbd{C-M-e}
(@code{end-of-defun}).

  If you wish to operate on the current defun, use @kbd{C-M-h}
(@code{mark-defun}) which puts point at the beginning and mark at the end
of the current or next defun.  For example, this is the easiest way to get
ready to move the defun to a different place in the text.  In C mode,
@kbd{C-M-h} runs the function @code{mark-c-function}, which is almost the
same as @code{mark-defun}; the difference is that it backs up over the
argument declarations, function name and returned data type so that the
entire C function is inside the region.

  Emacs assumes that any open-parenthesis found in the leftmost column
is the start of a defun.  Therefore, @b{never put an open-parenthesis
at the left margin in a Lisp file unless it is the start of a top
level list.  Never put an open-brace or other opening delimiter at the
beginning of a line of C code unless it starts the body of a
function.}  The most likely problem case is when you want an opening
delimiter at the start of a line inside a string.  To avoid trouble,
put an escape character (@samp{\}, in C and Emacs Lisp, @samp{/} in some
other Lisp dialects) before the opening delimiter.  It will not affect
the contents of the string.

  In the remotest past, the original Emacs found defuns by moving upward a
level of parentheses until there were no more levels to go up.  This always
required scanning all the way back to the beginning of the buffer, even for
a small function.  To speed up the operation, Emacs was changed to assume
that any @samp{(} (or other character assigned the syntactic class of
opening-delimiter) at the left margin is the start of a defun.  This
heuristic is nearly always right and avoids the costly scan.  GNU Emacs uses
the same convention.

@node[Grinding, MIDAS, Lisp Debug, Programs]

@section[Indentation for Programs]
@setref Grinding
@cindex{indentation}
@cindex{grinding}

  The best way to keep a program properly indented (``ground'') is to use
Emacs to re-indent it when it is changed.  Emacs has commands to indent
properly either a single line, a specified number of lines, or all of the
lines inside a single parenthetical grouping.

@c WideCommands
@table 7
@item @key(TAB)
Adjust indentation of current line.
@item @key(LFD)
Equivalent to @key(RET) followed by @key(TAB).
@item C-M-q
Re-indent all the lines within one list.
@item C-u @key(TAB)
Shift an entire list rigidly sideways so that its first line
is properly indented.
@item C-M-\
Re-indent all lines in the region.
@end table

@kindex{TAB}
@cfindex{c-indent-line}
@cfindex{lisp-indent-line}
  The basic indentation command is @key(TAB), which gives the current line
the correct indentation as determined from the previous lines.  The
function that @key(TAB) runs depends on the major mode; it is
@code{lisp-indent-line} in Lisp mode, @code{c-indent-line} in C mode, etc.
These functions understand different syntaxes for different languages, but
they all do about the same thing.  @key(TAB) in any programming language
major mode inserts or deletes whitespace at the beginning of the current
line, independent of where point is in the line.  If point is inside the
whitespace at the beginning of the line, @key(TAB) leaves it at the end of
that whitespace; otherwise, @key(TAB) leaves point fixed with respect to
the characters around it.

  Use @kbd{C-q @key(TAB)} to insert a tab at point.

@kindex{LFD}
@cfindex{newline-and-indent}
  When entering a large amount of new code, use @key(LFD)
(@code{newline-and-indent}), which is equivalent to a @key(RET) followed
by a @key(TAB).  @key(LFD) creates a blank line, and then gives it the
appropriate indentation.

  @key(TAB) indents the second and following lines of the body of an
parenthetical grouping each under the preceding one; therefore, if you
alter one line's indentation to be nonstandard, the lines below will tend
to follow it.  This is the right behavior in cases where the standard
result of @key(TAB) is unaesthetic.

@subsection{Indenting Several Lines}

  When you wish to re-indent code which has been altered or moved to a
different level in the list structure, you have several commands available.

@kindex{C-M-q}
@cfindex{indent-sexp}
@cfindex{indent-c-exp}
  You can re-indent the contents of a single list by positioning point
before the beginning of it and typing @kbd{C-M-q} (@code{indent-sexp} in
Lisp mode, @code{indent-c-exp} in C mode; also bound to other suitable
functions in other modes).  The indentation of the line the sexp
starts on is not changed; therefore, only the relative indentation within
the list, and not its position, is changed.  To correct the position as
well, type a @key(TAB) before the @kbd{C-M-q}.

@kindex{C-u TAB}
  If the relative indentation within a list is correct but the indentation
of its beginning is not, go to the line the list begins on and type
@kbd{C-u @key(TAB)}.  When @key(TAB) is given a numeric argument, it moves
all the lines in the grouping starting on the current line sideways the
same amount that the current line moves.  It is clever, though, and does
not move lines that start inside strings, or C preprocessor lines when in C
mode.

@kindex{C-M-Backslash}
@cfindex{indent-region}
  Another way to specify the range to be re-indented is with point and
mark.  The command @kbd{C-M-\} (@code{indent-region}) applies @key(TAB) to
every line whose first character is between point and mark.
 
@subsection[Customizing Lisp Indentation]
@cindex{customization}

  The indentation pattern for a Lisp expression can depend on the function
called by the expression.  For each Lisp function, you can choose among
several predefined patterns of indentation, or define an arbitrary one with
a Lisp program.

  The standard pattern of indentation is as follows: the second line of the
expression is indented under the first argument, if that is on the same
line as the beginning of the expression; otherwise, the second line is
indented underneath the function name.  Each following line is indented
under the previous line whose nesting depth is the same.

@vindex{lisp-indent-offset}
  If the variable @code{lisp-indent-offset} is non-@code{nil}, it overrides
the usual indentation pattern for the second line of an expression, so that
such lines are always indented @code{lisp-indent-offset} more columns than the
containing list.

@vindex{lisp-body-indention}
  The standard pattern is overridded for certain functions.  Functions
whose names start with @code{def} always indent the second line by
@code{lisp-body-indention} extra columns beyond the open-parenthesis
starting the expression.

  The standard pattern can be overridden in various ways for individual
functions, according to the @code{lisp-indent-hook} property of the
function name.  There are four possibilities for this property:
@table 3
@item nil
This is the same as no property; the standard indentation pattern is used.
@item defun
The pattern used for function names that start with @code{def} is used for
this function also.
@item @var[number]
The first @var[number] arguments of the function are @dfn{distinguished}
arguments; the rest are considered the @dfn{body} of the expression.
A line in the expression is indented according to whether the first
argument on it is distinguished or not.  If the argument is part of the
body, the line is indented @code{lisp-body-indent} more columns than the
open-parenthesis starting the containing expression.  If the argument is
distinguished andis either the first or second argument, it is indented
@i[twice] that many extra columns.  If the argument is distinguished and
not the first or second argument, the standard pattern is followed for that line.
@item @var[symbol]
@var[symbol] should be a function name; that function is called to
calculate the indentation of a line within this expression.  The function
receives two arguments:
@end table

@table 3
@item @var[state]
The value returned by @code{parse-partial-sexp} (a Lisp primitive for
indentation and nesting computation) when it parses up to the beginning of
this line.
@item @var[pos]
The position at which the line being indented begins.
@end table
@nopara
It should return either a number, which is the number of columns of
indentation for that line, or a list whose car is such a number.  The
difference between returning a number and returning a list is that a number
says that all following lines at the same nesting level should be indented
just like this one; a list says that following lines might call for
different indentations.  This makes a difference when the indentation is
being computed by @kbd{C-M-q}; if the value is a number, @kbd{C-M-q} need
not recalculate indentation for the following lines until the end of the
list.

@subsection[Customizing C Indentation]

  C does not have anything analogous to particular function names for which
special forms of indentation are desirable.  However, it has a different
need for customization facilities: many different styles of C indentation
are in common use.  There are six variables you can set to control the
style that Emacs C mode will use.

@vindex{c-indent-level}
  The variable @code{c-indent-level} controls the indentation for C
statements with respect to the surrounding block.  In the example
@example
    {
      foo ();
@end example
@nopara
the difference in indentation between the lines is @code{c-indent-level}.
Its standard value is 2.

If the open-brace beginning the compound statement is not at the beginning
of its line, the @code{c-indent-level} is added to the indentation of the line,
not the column of the  open-brace.  For example,
@example
if (losing) {
  do_this ();
@end example
One popular indentation style is that which results from setting
@code{c-indent-level} to 8 and putting open-braces at the end of a line in
this way.

@vindex{c-continued-statement-offset}
  @code{c-continued-statement-offset} controls the extra indentation for a
line that starts within a statement (but not within parentheses or
brackets).  These lines are usually statements that are within other
statements, such as the then-clauses of @code{if} statements and the bodies
of @code{while} statements.  This parameter is the difference in
indentation between the two lines in
@example
if (x == y)
  do_it ();
@end example
@nopara
Its standard value is 2.  Some popular indentation styles correspond to a
value of zero for @code{c-continued-statement-offset}.

@vindex{c-brace-offset}
  @code{c-brace-offset} is the extra indentation given to a line that
starts with an open-brace or close-brace.  Its standard value is zero;
compare
@example
if (x == y)
  {
@end example
@nopara
with
@example
if (x == y)
  do_it ();
@end example
@nopara
if @code{c-brace-offset} were set to 4, the first example would become
@example
if (x == y)
      {
@end example

@vindex{c-argdecl-indent}
  @code{c-argdecl-indent} controls the indentation of declarations of the
arguments of a C function.  It is absolute: argument declarations receive
exactly @code{c-argdecl-indent} spaces.  The standard value is 5, resulting
in code like this:
@example
char *
index (string, char)
     char *string;
     int char;
@end example

@vindex{c-label-offset}
  @code{c-label-offset} is the extra indentation given to a line thst
contains a label, a case statement, or a default statement.  Its standard
value is -2, resulting in code like this
@example
switch (c)
  {
  case 'x':
@end example
@nopara
If @code{c-label-offset} were zero, the same code would be indented as
@example
switch (c)
  {
    case 'x':
@end example
@nopara
This example assumes that the other variables above also have their
standard values.

  I strongly recommend that you try out the indentation style produced by
the standard settings of these variables, together with putting open
braces on separate lines.  You can see how it looks in all the C source
files of GNU Emacs.

@vindex{c-auto-newline}
  One other variable, @code{c-auto-newline}, does not affect the style of
indentation that is used, but makes insertion of certain characters insert
newlines automatically.  When this variable is non-@code{nil}, newlines
are inserted both before and after braces that you insert, and after
colons and semicolons.  Correct C indentation is done on all the lines
that are made this way.

@node[Matching, Comments, Indenting, Programs]

@section[Automatic Display Of Matching Parentheses]
@setref Matching
@cindex{matching parentheses}
@cindex{parentheses}

  The Emacs parenthesis-matching feature is designed to show automatically
how parentheses match in the text.  When ever a self-inserting character
that is a closing delimiter is typed, the cursor moves momentarily to the
location of the matching opening delimiter, provided that is on the screen.
If it is not on the screen, some text starting with that opening delimiter
is displayed in the echo area.  Either way, you can tell what grouping is
being closed off.

  In Lisp, automatic matching applies only to parentheses.  In C, it
applies to braces and brackets too.  Emacs knows which characters to regard
as matching delimiters based on the syntax table, which is set by the major
mode.  @xref[Syntax].

  If the opening delimiter and closing delimiter are mismatched---such as,
in @samp{[x)}---a warning message is displayed in the echo area.  The
correct matches are specified in the syntax table.

@vindex{blink-matching-paren}
@vindex{blink-matching-paren-distance}
  Two variables control parenthesis match display.
@code{blink-matching-paren} turns the feature on or off; @code{nil} turns
it off, but the default is @code{t} to turn match display on.
@code{blink-matching-paren-distance} specifies how many characters back to
search to find the matching opening delimiter.  If the match is not found
in that far, scanning stops, and nothing is displayed.  This is to prevent
scanning for the matching delimiter from wasting lots of time when there is
none.

@node[Comments, Lisp, Matching, Programs]

@section[Manipulating Comments]
@setref Comments
@cindex{comments}
@kindex{M-;}
@cindex{indentation}
@cfindex{indent-for-comment}

  The comment commands insert, kill and align comments.  There are also
commands for moving through existing code and inserting comments.

@c WideCommands
@table 7
@item M-;
Insert or align comment.
@item C-x ;
Set comment column.
@item C-u - C-x ;
Kill comment on current line.  With region, kill comments in region.
@item M-@key(LFD)
Like @key(RET) followed by inserting and aligning a comment-start string.
@end table

  The command that creates a comment is @kbd{Meta-;}
(@code{indent-for-comment}).  If there is no comment already on the line, a
new comment is created, aligned at a specific column called the
@dfn[comment column].  The comment is created by inserting whatever string
Emacs thinks should start comments in the current major mode.  Point is
left after the comment-starting string.  If the text of the line goes past
the comment column, then the indentation is done to a suitable boundary
(usually, at least one space is inserted).  If the major mode has specified
a string to terminate comments, that is inserted after point, to keep the
syntax valid.

  @kbd{Meta-;} can also be used to align an existing comment.  If a line
already contains the string that starts comments, then @kbd{M-;} just moves
point after it and re-indents it to the right column.  Exception: comments
starting in column 0 are not moved.  Also, in particular modes, there are
special rules for indenting certain kinds of comments in certain contexts.

  Even when an existing comment is properly aligned, @kbd{M-;} is still
useful for moving directly to the start of the comment.

@kindex{C-u - C-x ;}
@cfindex{kill-comment}
  @kbd{C-u - C-x ;} (@code{kill-comment}) kills the comment on the current line,
if there is one.  The indentation before the start of the comment is killed
as well.  If there does not appear to be a comment in the line, nothing is
done.  To reinsert the comment on another line, move to the end of that
line, do @kbd{C-y}, and then do @kbd{M-;} to realign it.  Note that
@kbd{C-u - C-x ;} is not a distinct key; it is
@kbd{C-x ;} (@code{set-comment-column}) with a negative argument.  That
command is programmed so that when it receives a negative argument it calls
@code{kill-comment}.  However, @code{kill-comment} is a valid command
which you could bind directly to a key if you wanted to.

@subsection[Multiple Lines of Comments]

  If you wish to align a large number of comments, give @kbd{Meta-;} an
argument, and it indents what comments exist on that many lines, creating
none.  Point is left after the last line processed (unlike the no-argument
case).

@kindex{M-LFD}
@cindex{blank lines}
@cindex{Auto Fill mode}
@cfindex{indent-new-comment-line}
  If you are typing a comment and find that you wish to continue it on
another line, you can use the command @kbd{Meta-@key(LFD)}
(@code{indent-new-comment-line}), which terminates the comment you are
typing, creates a new blank line afterward, and begins a new comment
indented under the old one.  When Auto Fill mode is on, going past the fill
column while typing a comment causes the comment to be continued in just
this fashion.  If point is not at the end of the line when
@kbd{M-@key(LFD)} is typed, the text on the rest of the line becomes
part of the new comment line.

@subsection[Double and Triple Semicolons in Lisp]

  In Lisp code there are conventions for comments which start with more
than one semicolon.  Comments which start with two semicolons are indented
as if they were lines of code, instead of at the comment column.  Comments
which start with three semicolons are supposed to start at the left margin.
Emacs understands these conventions by indenting a double-semicolon comment
using @key(TAB), and by not changing the indentation of a triple-semicolon
comment at all.  (Actually, this rule applies whenever the comment starter
is a single character and is duplicated).

@subsection[Options Controlling Comments]

@vindex{comment-column}
@kindex{C-x ;}
@cfindex{set-comment-column}
  The comment column is stored in the variable @code{comment-column}.  You
can set it to a number explicitly; note that the value is in pixels, not
characters.  Alternatively, the command @kbd{C-x ;}
(@code{set-comment-column}) sets the comment column to the column point is
at.  @kbd{C-u C-x ;} sets the comment column to match the last comment
before point in the buffer, and then does a @kbd{Meta-;} to align the
current line's comment under the previous one.  Note that @kbd{C-u - C-x ;}
runs the function @code{kill-comment} as described above.

@cindex{major modes}
  Many major modes supply default local values for the comment column.  Its
value is local to each buffer, so changing it in one buffer does not other
buffers.  @xref[Variables].

@vindex{comment-start-skip}
  The comment commands recognize comments based on the regular expression
that is the value of the variable @code{comment-start-skip}.  This regexp
should not match the null string.  It may match more than the comment
starting delimiter in the strictest sense of the word; for example, in C
mode the value of the variable is @code{@ttfont["/\\*+ *"]}, which matches
extra stars and spaces after the @samp{/*} itself.

@vindex{comment-start}
@vindex{comment-end}
  When a comment command makes a new comment, it inserts the value of
@code{comment-start} to begin it.  The value of @code{comment-end} is
inserted after point, so that it will follow the text that you will insert
into the comment.

@vindex{comment-multi-line}
  @code{comment-multi-line} controls how @kbd{M-@key(LFD)}
@code{indent-new-comment-line} behaves when used inside a comment.  If
@code{comment-multi-line} is @code{nil}, as it normally is, then the
comment on the starting line is terminated and a new comment is started on
the new following line.  If @code{comment-multi-line} is not @code{nil},
then the new following line is set up as part of the same comment that was
found on the starting line.  This is done by not inserting a terminator
there, and not inserting a starter on the new line.

@vindex{comment-indent-hook}
  The variable @code{comment-indent-hook} should contain a function that
will be called to compute the indentation for a newly inserted comment or
for aligning an existing comment.  It is set differently by various major
modes.  The function is called with no arguments, but with point at the
beginning of the comment, or at the end of a line if a new comment is to be
inserted.  It should return the column in which the comment ought to start.
In Lisp mode, the indent hook function can base its decision on how many
semicolons begin an existing comment, and on the code in the preceding lines.

@section{Editing Without Unbalanced Parentheses}

@table 7
@item M-(
Put parentheses around next sexp(s).
@item M-)
Move past next close parenthesis and re-indent.
@end table

@kindex{M-(}
@kindex{M-)}
@cfindex{insert-parentheses}
@cfindex{move-over-close-and-reindent}
  The commands @kbd{M-(} (@code{insert-parentheses}) and @kbd{M-)}
(@code{move-over-close-and-reindent}) are designed to facilitate a
style of editing which keeps parentheses balanced at all times.
@kbd{M-(} inserts a pair of parentheses, either together as in
@samp{()}, or, if given an argument, around the next several
sexps, and leaves point after the open parenthesis.  Instead
of typing @kbd{( F O O )}, you can type @kbd{M-( F O O}, which has
the same effect except for leaving the cursor before the close
parenthesis.  Then you would type @kbd{M-)}, which moves past the
close parenthesis, deleting any indentation preceding it (in this
example there is none), and indenting with @key(LFD) after it.

@node[Lisp Documentation, Lisp Debugging, Lisp Search, Programs]

@section[Documentation Commands]
@setref Lisp Documentation

@kindex{C-h f}
@cfindex{describe-function}
@kindex{C-h v}
@cfindex{describe-variable}
  As you edit Lisp code to be run in Emacs, the commands @kbd{C-h f}
(@code{describe-function}) and @kbd{C-h v} (@code{describe-variable}) can
be used to print documentation of functions and variables that you want to
call.  These commands use the minibuffer to read the name of a function or
variable to document, and display the documentation in a window.

  For extra convenience, these commands provide default arguments based on
the code in the neighborhood of point.  @kbd{C-h f} sets the default to the
function called in the innermost list containing point.  @kbd{C-h v} uses
the symbol name around or adjacent to point as its default.

@cfindex{manual-entry}
  Documentation on Unix commands, system calls and libraries can be
obtained with the @kbd{M-x manual-entry} command.  This reads a topic as an
argument, and displays the text on that topic from the Unix manual.
@code{manual-entry} always searches all 8 sections of the manual, and
concatenates all the entries that are found.  For example, the topic
@samp{termcap} finds the description of the termcap library from section 3,
followed by the description of the termcap data base from section 5.

@node[Change Log]

@section{Change Logs}

@cindex{change log}
@cfindex{add-change-log-entry}
  The Emacs command @kbd{M-x add-change-log-entry} helps you keep a record
of when and why you have changed a program.  It assumes that you have a
file in which you write a chronological sequence of entries describing
individual changes.  The default is to store the change entries in a file
called @code{ChangeLog} in the same directory as the file you are editing.
The same @code{ChangeLog} file therefore records changes in all the files
in the directory.

  A change log entry starts with a header line that contains a star
followed by your name and the current date.  Aside from these header lines,
every line in the change log starts with a tab.  One entry can describe
several changes; each change starts with a line starting with a tab and a
star.  @kbd{M-x add-change-log-entry} visits the change log file and
creates a new entry unless the most recent entry is for today's date and
your name.  In either case, it adds a new line to start the description of
another change just after the header line of the entry.  When @kbd{M-x
add-change-log-entry} is finished, all is prepared for you to edit in the
description of what you changed and how.  You must then save the change log
file yourself.

  The change log file is always visited in Indented Text mode, which means
that @key(LFD) and auto-filling indent each new line like the previous
line.  This is convenient for entering the contents of an entry, which must
all be indented.

  Here is an example of the formatting conventions used in the change log
for Emacs:
@example
Wed Jun 26 19:29:32 1985  Richard M. Stallman  (rms at mit-prep)

        * xdisp.c (try_window_id):
        If C-k is done at end of next-to-last line,
        this fn updates window_end_vpos and cannot leave
        window_end_pos nonnegative (it is zero, in fact).
        If display is preempted before lines are output,
        this is inconsistent.  Fix by setting
        blank_end_of_window to nonzero.

Tue Jun 25 05:25:33 1985  Richard M. Stallman  (rms at mit-prep)

        * cmds.c (Fnewline):
        Call the auto fill hook if appropriate.

        * xdisp.c (try_window_id):
        If dot is found by compute_motion after xp, record that
        permanently.  If display_text_line sets dot position wrong
        (case where like is killed, dot is at eob and that line is
        not displayed), detect and set it again in final compute_motion.
@end example

@section[Tag Tables]
@setref Tags
@cindex{tag table}

  A @dfn{tag table} is a description of how a multi-file program is broken
up into files.  It lists the names of the component files and the names and
positions of the functions in each file.  Grouping the related files makes
it possible to search or replace through all the files with one command.
Recording the function names and positions makes possible the @kbd{Meta-.}
command which you can use to find the definition of a function without
having to know which of the files it is in.

  Tag tables are stored in files called @dfn{tag table files}.  The
conventional name for a tag table file is @code{TAGS}.

  Just what names from the described files are recorded in the tag table
depends on the programming language of the described file.  They normally
include all functions and subroutines, and may also include global
variables, data types, and anything else convenient.  In any case, each
name recorded is called a @dfn{tag}.

  In Lisp code, any function defined with @code{defun}, any variable defined
with @code{defvar} or @code{defconst}, and in general the first argument of
any expression that starts with @code{(def} in column zero, is a tag.

  In C code, any C function is a tag, and so is any typedef if @code{-t} is
specified when the tag table is constructed.

  In Fortran code, functions and subroutines are tags.

@subsection{Creating Tag Tables}
@cindex{etags program}

  The @code{etags} program is used to create a tag table file.  It knows
the syntax of C, Fortran and Emacs Lisp.  To use @code{etags}, type
@example
etags @var[inputfiles]@dots
@end example
@nopara
as a shell command.  It reads the specified files and writes a tag table
named @code{TAGS} in the current working directory.  @code{etags}
recognizes the language used in an input file based on its file name and
contents; there are no switches for specifying the language.  The @code{-t}
switch tells @code{etags} to record typedefs in C code as tags.

  Each entry in the tag table records the name of one tag, the position in
its file of that tag's definition, and (implicitly) the name of the file
that tag is defined in.

  If the tag table data become outdated, due to changes in the files
described in the table, the way to update the tag table is the same way it
was made in the first place.  It is not necessary to do this often.

  If the tag table fails to record a tag, or records it for the wrong file,
then Emacs cannot possibly find its definition.  However, if the position
recorded in the tag table becomes a little bit wrong (due to some editing
in the file that the tag definition is in), the only consequence is to slow
down finding the tag slightly.  Even if the stored position is very wrong,
Emacs will still find the tag, but it must search the entire file for it.

  So you should update a tag table when you define new tags that you want
to have listed, or when you move tag definitions from one file to another,
or when changes become substantial.  Normally there is no need to update
the tag table after each edit, or even every day.

@subsection{Selecting a Tag Table}

@vindex{tags-file-name}
@cfindex{visit-tag-table}
  Emacs has at any time one @dfn{selected} tag table, and all the commands
for working with tag tables use the selected one.  To select a tag table,
type @kbd{M-x visit-tag-table}, which reads the tag table file name as an
argument.  The name @code{TAGS} in the default directory is used as the
default file name.

  All this command does is store the file name in the variable
@code{tags-file-name}.  Emacs does not actually read in the tag table
contents until you try to use them.  Setting this variable yourself is just
as good as using @code{visit-tag-table}.  The variable's initial value is
@code{nil}; this value tells all the commands for working with tag tables
that they must ask for a tag table file name to use.

@subsection{Finding a Tag}

  The most important thing that a tag table enables you to do is to find
the definition of a specific tag.

@table 7
@item M-. @var[tag]
Find first definition of @var[tag].
@item C-u M-.
Find next alternate definition of last tag specified.
@item C-x 4 . @var[tag]
Find first definition of @var[tag], but display it in another window.
@end table

@kindex{M-.}
@cfindex{find-tag}
  @kbd{M-.} (@code{find-tag}) is the command to find the definition of a
specified tag.  It searches through the tag table for that tag, as a
string, and then uses the tag table info to determine the file that the
definition is in and the approximate character position in the file of the
definition.  Then @code{find-tag} visits that file, moves point to the
approximate character position, and stars searching ever-increasing
distances away for the the text that should appear at the beginning of the
definition.

  If an empty argument is given (just type @key(RET)), the sexp in the
buffer before or around point is used as the name of the tag to find.
@xref[Lists], for info on sexps.

  The argument to @code{find-tag} need not be the whole tag name; it can be a
substring of a tag name.  However, there can be many tag names containing
the substring you specify.  Since @code{find-tag} works by searching the
text of the tag table, it finds the first tag in the table that the
specified substring appears in.  The way to find other tags that match the
substring is to give @code{find-tag} a numeric argument, as in @kbd{M-0
M-.}; this does not read a tag name, but continues searching the tag
table's text for another tag containing the same substring last used.

@kindex{C-x 4 .}
@cfindex{find-tag-other-window}
  Like most commands that can switch buffers, @code{find-tag} has another
similar command that displays the new buffer in another window.  @kbd{C-x 4
.} invokes the function @code{find-tag-other-window}.

@subsection[Searching and Replacing with Tag Tables]

  The commands in this section visit and search all the files listed in the
selected tag table, one by one.

@table 7
@item M-x tags-search
Search for the specified regexp through the files in the selected tag
table.
@item M-x tags-query-replace
Perform a @code{query-replace} on each file in the selected tag table.
@item M-,
Restart one of the commands above, from the current location of point
(@kbd{M-@key(COMMA)}, @code{tags-loop-continue}).
@end table

@cfindex{tags-search}
  @kbd{M-x tags-search} reads a regexp using the minibuffer, then visits
the files of the selected tag table one by one, and searches through each
one for that regexp.  As soon as an occurrence is found, @code{tags-search}
returns.

@kindex{M-,}
@cfindex{tags-loop-continue}
  Having found one match, you probably want to find all the rest.  To find
one more match, type @kbd{M-,} (@code{tags-loop-continue}) to resume the
@code{tags-search}.  This searches the rest of the current buffer,
followed by the remaining files of the tag table.

@cfindex{tags-query-replace}
  @kbd{M-x tags-query-replace} performs a single @code{query-replace}
through all the files in the tag table.  It reads a string to search for
and a string to replace with, just like ordinary @kbd{M-x query-replace}.
It searches much like @kbd{M-x tags-search} but repeatedly, processing
matches according to your input.  @xref[Replace], for more information on
@code{query-replace}.

  It is possible to get through all the files in the tag table with a single
invocation of @kbd{M-x tags-query-replace}.  But since any unrecognized
character causes the command to exit, you may need to continue where you
left off.  @kbd{M-,} can be used for this.  @kbd{M-,} resumes the last
tags search or replace command that you did.

@subsection[Stepping Through a Tag Table]
@cfindex{next-file}

  If you wish to process all the files in the selected tag table, but @kbd{M-x tags
search} and @kbd{M-x tags-query-replace} in particular are not what you
want, you can use @kbd{M-x next-file}.

@table 7
@item M-x next-file
Visit the next file in the selected tag table.
@item C-u M-x next-file
With a numeric argument, regardless of its value, visit the first
file in the tag table.
@end table

@subsection{Tag Table Inquiries}

@table 7
@item M-x list-tags
Display a list of the tags defined in a specific program file.
@item M-x tags-apropos
Display a list of all tags matching a specified regexp.
@end table

@cfindex{list-tags}
  @kbd{M-x list-tags} reads the name of one of the files described by the
selected tag table, and displays a list of all the tags defined in that
file.  The ``file name'' argument is really just a string to compare
against the names recorded in the tag table; it is read as a string rather
than as a file name.  Therefore, completion and defaulting are not
available, and you must enter the string the same way it appears in the tag
table.  Do not include a directory as part of the file name unless the file
name recorded in the tag table includes a directory.

@cfindex{tags-apropos}
  @kbd{M-x tags-apropos} is like @code{apropos} for tags.  It reads a
regexp, then finds all the tags in the selected tag table whose entries
match that regexp, and displays the tag names found.

@chapter[Compiling, Running and Testing Programs]
@setref Running

  The previous chapter discusses the Emacs commands that are useful for
making changes in programs.  This chapter deals with commands that assist
in the larger process of developing and maintaining programs.

@node[Compilation]
@section[Running `make' or Other Compilers]
@setref Compilation
@cindex{inferior process}
@cindex{make}
@cindex{compilation errors}
@cindex{error log}

  Emacs can run compilers for noninteractive languages such as C and
Fortran as inferior processes, feeding the error log into an Emacs buffer.
It can also parse the error messages and visit the files in which errors
are found, moving point right to the line where the error occurred.

@cfindex{compile}
  To run @code{make} or other compiler, do @kbd{M-x compile}.  This command
reads a shell command line using the minibuffer, and then executes the
specified command line in an inferior shell with output going to the buffer
named @code{*compilation*}.  The current buffer's default directory is used
as the working directory for the execution of the command; normally,
therefore, the makefile comes from this directory.

@vindex{compile-command}
  When the shell command line is read, the minibuffer appears containing a
default command line, which is the command you used the last time you did
@kbd{M-x compile}.  If you type just @key(RET), the same command line is
used again.  The first @kbd{M-x compile} provides @code{make -k} as the
default.  The default is taken from the variable @code{compile-command}; if
the appropriate compilation command for a file is something other than
@code{make -k}, it can be useful to have the file specify a local value for
@code{compile-command} (@pxref[Variables]).

  Starting a compilation causes the buffer @code{*compilation*} to be
displayed in another window but not selected.  The mode line tells you
whether compilation is finished, with the word @samp{run} or @samp{exit}
inside the parentheses.  You do not have to keep this buffer visible;
compilation continues in any case.

@cfindex{kill-compilation}
  To kill the compilation process, do @kbd{M-x kill-compilation}.  You will
see that the mode line of the @code{*compilation*} buffer changes to say
@samp{signal} instead of @samp{run}.  Starting a new compilation also kills
any running compilation, as only one can exist at any time.  However, this
requires confirmation before actually killing a compilation that is running.

@kindex{C-x `}
@cfindex{next-error}
  To parse the compiler error messages, type @kbd{C-x `}
(@code{next-error}).  It displays the buffer @code{*compilation*} in one
window and the buffer in which the next error occurred in another window.
Point in that buffer is moved to the line where the error was found.
The corresponding error message is scrolled to the top of the window in
which @code{*compilation*} is displayed.

  The first time @kbd{C-x `} is used, after the start of a compilation, it
parses all the error messages, visits all the files that have error
messages, and makes markers pointing at the lines that the error messages
refer to.  Then it moves to the first error message location.  Subsequent
uses of @kbd{C-x `} advance down the data set up by the first use.  When
the preparsed error messages are exhausted, the next @kbd{C-x `} checks for
any more error messages that have come in; this is useful if you start
editing the compiler errors while the compilation is still going on.  If no
more error messages have come in, @kbd{C-x `} gets a Lisp error.

  @kbd{C-u C-x `} discards the preparsed error message data and parses the
@code{*compilation*} buffer over again, then displaying the first error.
This way, you can process the same set of errors again.

@node[Lisp Modes]
@section{Major Modes for Lisp}
@setref Lisp Modes

  Emacs has four different major modes for Lisp.  They are the same in
terms of editing commands, but differ in the commands for executing Lisp
expressions.

@table 1
@item Emacs-Lisp mode
The mode for editing source files of programs to run in Emacs Lisp.
This mode defines @kbd{C-M-x} to evaluate the current defun.
@xref[Lisp Libraries].
@item Lisp Interaction mode
The mode for an interactive session with Emacs Lisp.  It defines
@key(LFD) to evaluate the sexp before point and insert its value in the
buffer.  @xref[Lisp Interaction].
@item Lisp mode
The mode for editing source files of programs that run in Lisps other
than Emacs Lisp.  This mode defines @kbd{C-M-x} to send the current defun
to an inferior Lisp process.  @xref[External Lisp].
@item Inferior Lisp mode
The mode for an interactive session with an inferior Lisp process.
@end table

@node[Lisp Libraries]

@section[Libraries of Lisp Code for Emacs]
@setref Lisp Libraries
@cindex{libraries}
@cindex{loading Lisp code}

  Lisp code for Emacs editing commands is stored in files whose names
conventionally end in @code{.el}.  This ending tells Emacs to edit them in
Emacs-Lisp mode, so you can use the @kbd{C-M-x} command described in the
following section to install changed functions.

@cfindex{load}
  Only the maintainers of such a file will want to edit its contents or
evaluating text from it, but every user must be able to load the file.
This is done with @kbd{M-x load}.

  @kbd{M-x load} reads a file name using the minibuffer and executes the
specified file as Lisp code.  But it has an important difference from all
other Emacs commands that read file names: it searches a sequence of
directories, and tries three file names in each directory.

  The argument you give to @kbd{M-x load} is usually not the full file
name.  Usually you omit the @code{.el} that the file name ends in.
@kbd{M-x load} tries three file names in each directory: first, the name
you specified; second, that name with @code{.elc} appended; third, that
name with @code{.el} appended.  A @code{.elc} file would be the result of
compiling the Lisp file into byte code; it is loaded if possible in
preference to the Lisp file itself because the compiled file will load and
run faster.

@vindex{load-path}
  The sequence of directories searched by @kbd{M-x load} is specified by
the variable @code{load-path}, a list of strings that are directory names.
Normally the first element of this list is @code{nil}, which means to
search the current default directory at that point; the remaining elements
are the names of the directories in which the Lisp code of Emacs itself is
stored.  Therefore, you can load an installed Emacs library without having
to specify a directory name.

  Often you do not have to run the @code{load} command yourself, because
the commands in a library have permanent definitions to @dfn{autoload}
that library.  Running any of those commands causes @code{load} to be
called to load the library; this replaces the autoload definitions with
the real ones from the library.

@cfindex{byte-compile-file}
  The way to make a byte-code compiled file from an Emacs-Lisp source file
is with @kbd{M-x byte-compile-file}.  The default argument for this
function is the file visited in the current buffer.  It reads the specified
file, compiles it into byte code, and writes an output file whose name is
made by appending @code{c} to the input file name.  Thus, the file
@code{rmail.el} would be compiled into @code{rmail.elc}.

@cfindex{byte-recompile-directory}
  To recompile the changed Lisp files in a directory, use @kbd{M-x
byte-recompile-directory}.  Specify just the directory name as an argument.
Each @code{.el} file that has been byte-compiled before is byte-compiled again if
it has changed since the previous compilation.  A numeric argument to this
command tells it to offer to compile each @code{.el} file that has not
already been compiled.  You must answer @kbd{Y} or @kbd{N} to each offer.

@cindex{mocklisp}
  GNU Emacs can run Mocklisp files by converting them to Emacs Lisp first.
To convert a Mocklisp file, visit it and then type @kbd{M-x
convert-mocklisp-buffer}.  Then save the resulting buffer of Lisp code in a
file whose name ends in @code{.el} and use the new file as a Lisp library.

@node[Lisp Eval]

@section[Evaluating Emacs-Lisp Expressions]
@setref Lisp Eval
@cindex{Emacs-Lisp mode}

@cfindex{emacs-lisp-mode}
  Lisp programs intended to be run in Emacs should be edited in Emacs-Lisp
mode; normally this will happen based on the file name that ends in
@code{.el}.  By contrast, Lisp mode itself is used for editing Lisp
programs intended for other Lisp systems.  Emacs-Lisp mode can be selected
with the command @kbd{M-x emacs-lisp-mode}.

  For testing of Lisp programs to run in Emacs, it is useful to be able
to evaluate part of the program as it is found in the Emacs buffer.  For
example, after changing the text of a Lisp function definition, evaluating
the definition installs the change for future calls to the function.
Evaluation of Lisp expressions is also useful in any kind of editing task
for invoking noninteractive functions (functions that are not commands).

@table 7
@item M-@key(ESC)
Read a Lisp expression in the minibuffer, evaluate it, and print the value
in the minibuffer.
@item C-x C-e
Evaluate the Lisp expression before point, and print the value in the
minibuffer.
@item C-M-x
Evaluate the defun containing or after point, and print the value in the
minibuffer.
@item M-x eval-region
Evaluate all the Lisp expressions in the region.
@item M-x eval-current-buffer
Evaluate all the Lisp expressions in the buffer.
@end table

@kindex{M-ESC}
@cfindex{eval-expression}
  @kbd{M-@key(ESC)} (@code{eval-expression}) is the most basic command
for evaluating a Lisp expression interactively.  It reads the expression
using the minibuffer, so you can execute any expression on a buffer
regardless of what the buffer contains.  When the expression is evaluated,
the current buffer is once again the buffer that was current when
@kbd{M-@key(ESC)} was typed.

  @kbd{M-@key(ESC)} can easily confuse users who do not understand it,
especially on keyboards with autorepeat where it can result from holding
down the @key(ESC) key for too long.  Therefore, @code{eval-expression}
is normally a disabled command.  Attempting to use this command asks for
confirmation and gives you the option of enabling it; once you enable the
command, confirmation will no longer be required for it.  @xref[Disabling].

@kindex{C-M-x}
@cfindex{eval-defun}
  In Emacs-Lisp mode, the key @kbd{C-M-x} is bound to the function
@code{eval-defun}, which parses the defun containing or following point as
a Lisp expression and evaluates it.  The value is printed in the echo area.
This command is convenient for installing in the Lisp environment changes
that you have just made in the text of a function definition.

@kindex{C-x C-e}
@cfindex{eval-last-sexp}
  The command @kbd{C-x C-e} (@code{eval-last-sexp}) performs a similar job
but is available in all major modes, not just Emacs-Lisp mode.  It finds
the sexp before point, reads it as a Lisp expression, evaluates it, and
prints the value in the echo area.  It is sometimes useful to type in an
expression and then, with point still after it, type @kbd{C-x C-e}.

  If @code{eval-defun} or @kbd{C-x C-e} is given a numeric argument, it
prints the value by insertion into the current buffer at point, rather than
in the echo area.  The argument value does not matter.

@cfindex{eval-region}
@cfindex{eval-current-buffer}
  The most general command for evaluating Lisp expressions from a buffer is
@code{eval-region}.  @kbd{M-x eval-region} parses the text of the region as
one or more Lisp expressions, evaluating them one by one.  @kbd{M-x
eval-current-buffer} is similar but evaluates the entire buffer.  This is a
reasonable way to install the contents of a file of Lisp code that you are
just ready to test.  After finding and fixing a bug, use @kbd{C-M-x} on
each function that you change, to keep the Lisp world in step with the
source file.

@node[Lisp Debug, Grinding, Lisp Documentation, Programs]

@section[The Lisp Debugger]
@setref Lisp Debug
@cindex{debugger}

@vindex{debug-on-error}
@vindex{debug-on-quit}
  GNU Emacs contains a debugger for Lisp programs executing inside it.
This debugger is normally not used; many commands frequently get Lisp
errors when invoked in inappropriate contexts (such as @kbd{C-f} at the end
of the buffer) and it would be very unpleasant for that to enter a special
debugging mode.  When you want to make Lisp errors invoke the debugger, you
must set the variable @code{debug-on-error} to non-@code{nil}.  Quitting
with @kbd{C-g} is not considered an error, and @code{debug-on-error} has no
effect on the handling of @kbd{C-g}.  However, if you set
@code{debug-on-quit} non-@code{nil}, @kbd{C-g} will invoke the debugger.
This can be useful for debugging an infinite loop; type @kbd{C-g} once the
loop has had time to reach its steady state.  @code{debug-on-quit} has no
effect on errors.

@cfindex{debug-on-entry}
@cfindex{cancel-debug-on-entry}
@cfindex{debug}
  You can also cause the debugger to be entered when a specified function
is called, or at a particular place in Lisp code.  Use @kbd{M-x
debug-on-entry} with argument @var[fun-name] to cause function
@var[fun-name] to enter the debugger as soon as it is called.  Use
@kbd{M-x cancel-debug-on-entry} to make the function stop entering the
debugger when called.  (Redefining the function also does this.)  To enter
the debugger from some other place in Lisp code, you must insert the
expression @code{debug} there and install the changed code with
@kbd{C-M-x}.  @xref[Lisp Eval].

  When the debugger is entered, it displays the previously selected buffer
in one window and a backtrace buffer in another window.  The backtrace
buffer contains one line for each level of Lisp function execution
currently going on.  At the beginning of this buffer is a message
describing the reason that the debugger was invoked (such as, what error
message if it was invoked due to an error).

  The backtrace buffer is read-only, and is in a special major mode,
Backtrace mode, in which letters are defined as debugger commands.  The
usual Emacs editing commands are available; you can switch windows to
examine the buffer that was being edited at the time of the error, and you
can also switch buffers. visit files, and do any other sort of editing.
However, the debugger is a recursive editing level (@pxref[Recursive Edit])
and it is wise to go back to the backtrace buffer and exit the debugger
officially when you don't want to use it any more.

@cindex{current stack frame}
  The contents of the backtrace buffer show you the functions that are
executing and the arguments that were given to them.  It has the additional
purpose of allowing yo to specify a stack frame by moving point to the line
describing that frame.  The frame whose line point is on is considered the
@dfn{current frame}.  Some of the debugger commands operate on the current
frame.  Debugger commands are mainly used for stepping through code an
expression at a time.  Here is a list of them.

@table 7
@item b
Set up to enter the debugger when the current frame is exited.  Frames that
will invoke the debugger on exit are marked with stars.
@item c
Exit the debugger and continue execution.  If the debugger was entered due
to an error or quit, the usual error or quit handling takes place after you
exit the debugger.  Otherwise, execution continues.
@item d
Continue execution, but enter the debugger the next time a Lisp function is
called.  This allows you to step through the subexpressions of an
expression, seeing what values the subexpressions compute and what else
they do

The stack frame made for the function call which enters the debugger in
this way will be marked automatically for the debugger to be called when
the frame is exited.  You can use the @kbd{u} command to cancel this mark.
@item e
Read a Lisp expression in the minibuffer, evaluate it, and print the value
in the echo area.  The same as the command @kbd{M-@key(ESC)}, except
that @kbd{e} is not normally disabled like @kbd{M-@key(ESC)}.
@item u
Don't enter the debugger when the current frame is exited.  This cancels a
@kbd{b} command on that frame.
@item q
Terminate the program being debugged; return to top level Emacs command execution.
@item r
Return a value from the debugger.  The value is computed by reading an
expression with the minibuffer and evaluating it.  The value returned by
the debugger makes a difference when the debugger was invoked due to exit
from a Lisp call frame (as requested with @kbd{b}); then the value
specified in the @kbd{r} command is used as the value of that frame.
@end table

@node[Lisp Interaction]
@section[Lisp Interaction Buffers]
@setref Lisp Interaction

  The buffer @code{*scratch*} which is selected when Emacs starts up is
provided for evaluating Lisp expressions interactively inside Emacs.  Both
the expressions you evaluate and their output goes in the buffer.

  The @code{*scratch*} buffer's major mode is Lisp Interaction mode, which
is the same as Emacs-Lisp mode except for one command, @key(LFD).  In
Emacs-Lisp mode, @key(LFD) is an indentation command, as usual.  In
Lisp Interaction mode, @key(LFD) is bound to
@code{eval-print-last-sexp}.  This function reads the Lisp expression
before point, evaluates it, and inserts the value in printed representation
before point.

  Thus, the way to use the @code{*scratch*} buffer is to insert Lisp
expressions at the end, ending each one with @key(LFD) so that it will be
evaluated.  The result is a complete typescript of the expressions you have
evaluated and their values.

@cfindex{lisp-interaction-mode}
  The rationale for this feature is that Emacs must have a buffer when it
starts up, but that buffer is not useful for editing files since a new
buffer is made for every file that you visit.  The Lisp interpreter
typescript is the most useful thing I can think of for the initial buffer
to do.  @kbd{M-x lisp-interaction-mode} will put any buffer in Lisp
Interaction mode.

@section[Running an External Lisp]
@setref External Lisp

  Emacs has facilities for running programs in other Lisp systems.
You can run a Lisp process as an inferior of Emacs, and pass expressions to
it to be evaluated.  You can also pass changed function definitions
directly from the Emacs buffers in which you edit the Lisp programs to the
inferior Lisp process.

@cfindex{run-lisp}
  To run an inferior Lisp process, type @kbd{M-x run-lisp}.  This runs the
program named @code{lisp}, the same program you would run by typing
@code{lisp} as a shell command, with both input and output going through
an Emacs buffer named @code{*lisp*}.  That is to say, any ``terminal
output'' from Lisp will go into the buffer, advancing point, and any
``terminal input'' for Lisp comes from text in the buffer.  To give input
to Lisp, go to the end of the buffer and type the input, terminated by
@key(RET).  The @code{*lisp*} buffer is in Inferior Lisp mode, a mode
which has all the special characteristics of Lisp mode and Shell mode
(@pxref[Shell]).

@cfindex{lisp-mode}
  For the source files of programs to run in external Lisps, use Lisp
mode.  This mode can be selected with @kbd{M-x lisp-mode}, and is used
automatically for files whose names end in @code{.l} or @code{.lisp}, as
most Lisp systems usually expect.

@kindex{C-M-x}
@cfindex{lisp-send-defun}
  When you edit a function in a Lisp program you are running, the easiest
way to send the changed definition to the inferior Lisp process is the key
@kbd{C-M-x}.  In Lisp mode, this runs the function @code{lisp-send-defun},
which finds the defun around or following point and sends it as input to
the Lisp process.  (Emacs can send input to any inferior process regardless
of what buffer is current.)

  Contrast the meanings of @kbd{C-M-x} in Lisp mode (for editing programs
to be run in another Lisp system) and Emacs-Lisp mode (for editing Lisp
programs to be run in Emacs): in both modes it has the effect of installing
the function definition that point is in, but the way of doing so is
different according to where the relevant Lisp environment is found.
@xref[Lisp Modes].

@node[Abbrevs]

@chapter[Abbrevs]
@setref Abbrevs
@cindex{abbrevs}
@cindex{expansion (of abbrevs)}

  An @dfn{abbrev} is a word which changes (@dfn{expands}), if you
insert it, into some different text.  Abbrevs are defined by the user to
expand in specific ways.  For example, you might define @samp{foo} as an
abbrev expanding to @samp{find outer otter}.  With this abbrev defined, you
would be able to get @samp{find outer otter } into the buffer by typing
@kbd{f o o @key(SPC)}.

@cfindex{abbrev-mode}
@vindex{abbrev-mode}
  Abbrevs expand only when Abbrev mode (a minor mode) is enabled.
Disabling Abbrev mode does not cause abbrev definitions to be
forgotten, but they do not expand until Abbrev mode is enabled again.
The command @kbd{M-x abbrev-mode} toggles Abbrev mode; with a
numeric argument, it turns Abbrev mode on if the argument is positive,
off otherwise.  @xref[Minor Modes].  @code{abbrev-mode} is also a
variable; Abbrev mode is on when the variable is non-@code{nil}.

  Abbrev definitions can be @dfn{mode-specific}---active only in one major
mode.  Abbrevs can also have @dfn{global} definitions that are active in
all major modes.  The same abbrev can have a global definition and various
mode-specific definitions for different major modes.  A mode specific
definition for the current major mode overrides a global definition.

  Abbrevs can be defined interactively during the editing session.
Lists of abbrev definitions can also be saved in files and reloaded in
later sessions.  Some users keep extensive lists of abbrevs that they load
in every session.

@section[Defining Abbrevs]

@table 7
@item C-x +
Define an abbrev to expand into some text before point.
@item C-x C-a
Similar, but define an abbrev available only in the current major mode.
@item M-x inverse-add-global-abbrev
Define a word in the buffer as an abbrev.
@item M-x inverse-add-mode-abbrev
Define a word in the buffer as a mode-specific abbrev.
@item M-x kill-all-abbrevs
After this command, there are no abbrev definitions in effect.
@end table

@kindex{C-x +}
@cfindex{add-global-abbrev}
  The usual way to define an abbrev is to enter the text you want the
abbrev to expand to, position point after it, and type @kbd{C-x +}
(@code{add-global-abbrev}).  This reads the abbrev itself using the
minibuffer, and then defines it as an abbrev for one or more words before
point.  Use a numeric argument to say how many words before point should be
taken as the expansion.  For example, to define the abbrev @samp{foo} as
mentioned above, insert the text @samp{find outer otter} and then type
@kbd{C-u 3 C-x + f o o @key(RET)}.

  An argument of zero to @kbd{C-x +} means to use the contents of the
region as the expansion of the abbrev being defined.

@kindex{C-x C-a}
@cfindex{add-mode-abbrev}
  The command @kbd{C-x C-a} (@code{add-mode-abbrev}) is similar, but
defines a mode-specific abbrev.  Mode specific abbrevs are active only in a
particular major mode.  @kbd{C-x C-a} defines an abbrev for the major mode
in effect at the time @kbd{C-x C-a} is typed.  The arguments work the same
as for @kbd{C-x +}.

@kindex{C-x -}
@cfindex{inverse-add-global-abbrev}
@kindex{C-x C-h}
@cfindex{inverse-add-mode-abbrev}
  If the text of the abbrev you want is already in the buffer instead of
the expansion, use command @kbd{C-x -} (@code{inverse-add-global-abbrev})
instead of @kbd{C-x +}, or use @kbd{C-x C-h}
(@code{inverse-add-mode-abbrev}) instead of @kbd{C-x C-a}.  These commands
are called ``inverse'' because they invert the meaning of the argument
found in the buffer and the argument read using the minibuffer.

  To change the definition of an abbrev, just add the new definition.  You
will be asked to confirm if the abbrev has a prior definition.  To remove
an abbrev definition, give a negative argument to @kbd{C-x +} or @kbd{C-x
C-a}.  You must choose the command to specify whether to kill a global
definition or a mode-specific definition for the current mode, since those
two definitions are independent for one abbrev.

@cfindex{kill-all-abbrevs}
  @kbd{M-x kill-all-abbrevs} removes all the abbrev definitions there are.

@section[Controlling Abbrev Expansion]
@setref Controlling Expansion

  An abbrev expands whenever it is present in the buffer just before point
and a self-inserting punctuation character (@key(SPC), comma,
etc.) is typed.  Most often the way an abbrev is used is to insert the
abbrev followed by punctuation.

@vindex{abbrev-all-caps}
  Abbrev expansion preserves case; thus, @samp{foo} expands into @samp{find
outer otter}; @samp{Foo} into @samp{Find outer otter}, and @samp{FOO} into
@samp{FIND OUTER OTTER} or @samp{Find Outer Otter} according to the
variable @code{abbrev-all-caps} (a non-@code{nil} value chooses the
first of the two expansions).

  These two commands are used to control abbrev expansion:

@table 7
@item M-'
Separate a prefix from a following abbrev to be expanded.
@item M-x unexpand-abbrev
Undo last abbrev expansion.
@item M-x expand-region-abbrevs
Expand some or all abbrevs found in the region.
@end table

@kindex{M-'}
@cfindex{abbrev-prefix-mark}
  You may wish to expand an abbrev with a prefix attached; for example, if
@samp{cnst} expands into @samp{construction}, you might want to use it to
enter @samp{reconstruction}.  It does not work to type @kbd{recnst},
because that is not necessarily a defined abbrev.  What does work is to use
the command @kbd{M-'} (@code{abbrev-prefix-mark}) in between the
prefix @samp{re} and the abbrev @samp{cnst}.  First, insert @samp{re}.
Then type @kbd{M-'}; this inserts a minus sign in the buffer to indicate
that it has done its work.  Then insert the abbrev @samp{cnst}; the buffer
now contains @samp{re-cnst}.  Now insert a punctuation character to expand
the abbrev @samp{cnst} into @samp{construction}.  The minus sign is deleted
at this point, because @kbd{M-'} left word for this to be done.  The
resulting text is the desired @samp{reconstruction}.

  If you actually want the text of the abbrev in the buffer, rather than
its expansion, you can accomplish this by inserting the following
punctuation with @kbd{C-q}.  Thus, @kbd{foo C-q -} leaves @samp{foo-} in
the buffer.

@cfindex{unexpand-abbrev}
  If you expand an abbrev by mistake, you can undo the expansion (replace
the expansion by the original abbrev text) with @kbd{M-x unexpand-abbrev}.
@kbd{C-_} (@code{undo}) can also be used to undo the expansion; but first
it will undo the insertion of the following punctuation character!

@cfindex{expand-region-abbrevs}
  @kbd{M-x expand-region-abbrevs} searches through the region for defined
abbrevs, and for each one found offers to replace it with its expansion.
This command is useful if you have typed in text using abbrevs but forgot
to turn on Abbrev mode first.  It may also be useful together with a
special set of abbrev definitions for making several global replacements at
once.

@section[Examining and Altering Abbrevs]

@table 7
@item M-x list-abbrevs
Print a list of all abbrev definitions.
@item M-x edit-abbrevs
Edit a list of abbrevs; you can add, alter or remove definitions.
@end table

@cfindex{list-abbrevs}
  The output from @kbd{M-x list-abbrevs} looks like this (after removing
blank lines of no significance):
@example
(lisp-mode-abbrev-table)
"dk"	       0    "define-key"
(global-abbrev-table)
"dfn"	       0    "definitions"
@end example
@nopara
(Some blank lines of no semantic significance, and some other abbrev
tables, have been omitted.)

  A line containing a name in parentheses is the header for abbrevs in a
particular abbrev table; @code{global-abbrev-table} contains all the global
abbrevs, and the other abbrev tables that arenamed after major modes
contain the mode-specific abbrevs.

  Within each abbrev table, each nonblank line defines one abbrev.  The
word at the beginning is the abbrev.  The number that appears is the number
of times the abbrev has been expanded.  Emacs keeps track of this to help
you see which abbrevs you actually use, in case you decide to eliminate
those that you don't use often.  The string at the end of the line is the
expansion.

@cfindex{edit-abbrevs}
@kindex{C-x C-s}
@cfindex{edit-abbrevs-redefine}
  @kbd{M-x edit-abbrevs} allows you to add, change or kill abbrev
definitions by editing a list of them in an Emacs buffer.  The list has
the same format described above.  The buffer of abbrevs is called
@code{*Abbrevs*}, and is in Edit-Abbrevs mode.  This mode redefines the
key @kbd{C-x C-s} to install the abbrev definitions as specified in
the buffer.  The command that does this is @code{edit-abbrevs-redefine}.
Any abbrevs not described in the buffer are eliminated when this is done.

  @code{edit-abbrevs} is actually the same as @code{list-abbrevs} except
that it selects the buffer @code{*Abbrevs*} whereas @code{list-abbrevs}
merely displays it in another window.

@section[Saving Abbrevs]

  These commands allow you to keep abbrev definitions between editing
sessions.

@table 7
@item M-x write-abbrev-file
Write a file describing all defined abbrevs.
@item M-x read-abbrev-file
Read such a file and define abbrevs as specified there.
@item M-x quietly-read-abbrev-file
Similar but do not display a message about what is going on.
@item M-x define-abbrevs
Define abbrevs from buffer.
@item M-x insert-abbrevs
Insert all abbrevs and their expansions into the buffer.
@end table

@cfindex{write-abbrev-file}
  @kbd{M-x write-abbrev-file} reads a file name using the minibuffer and
writes a description of all current abbrev definitions into that file.  The
text stored in the file looks like the output of @kbd{M-x list-abbrevs}.

@cfindex{read-abbrev-file}
@cfindex{quietly-read-abbrev-file}
@vindex{abbrev-file-name}
  @kbd{M-x read-abbrev-file} reads a file name using the minibuffer and
reads the file, defining abbrevs according to the contents of the file.
@kbd{M-x quietly-read-abbrev-file} is the same except that it does not
display a message in the echo area saying that it is doing its work.
If an empty argument is given to either of these functions, the file name
used is the value of the variable @code{abbrev-file-name}, which is by
default @code{"~/.abbrev_defs"}.

  These commands are used to save abbrev definitions for use in a
later session.

@vindex{save-abbrevs}
  Emacs will offer to save abbrevs automatically if you have changed any of
them, whenever it offers to save all files (for @kbd{C-x s} or @kbd{C-x
C-c}).  This feature can be inhibited by setting the variable
@code{save-abbrevs} to @code{nil}.

@cfindex{insert-abbrevs}
@cfindex{define-abbrevs}
  The commands @kbd{M-x insert-abbrevs} and @kbd{M-x define-abbrevs} are
similar to the previous commands but work on text in an Emacs buffer.
@kbd{M-x insert-abbrevs} inserts text into the current buffer before point,
describing all current abbrev definitions; @kbd{m-x define-abbrevs} parses
the entire current buffer and defines abbrevs accordingly.

@node[PICTURE, Sort, Bugs, Top]

@chapter[Editing Pictures]
@cindex{pictures}
@cfindex{edit-picture}

  If you want to create a picture made out of text characters (for
example, a picture of the division of a register into fields, as a
comment in a program), use @code{edit-picture} to enter Picture mode.

  In Picture mode, editing is based on the @dfn{quarter-plane} model of
text, according to which the text characters lie studded on an
area that stretches infinitely far to the left and downward.  The concept
of the end of a line does not exist in this model; the most you can say is
where the last nonblank character on the line is found.

  Of course, Emacs really always considers text as a sequence of
characters, and lines really do have ends.  But in Picture mode most
frequently-used keys are rebound to commands that simulate the
quarter-plane model of text.  They do this by inserting spaces or by
converting tabs to spaces.

  Most of the basic editing commands of Emacs are redefined by Picture mode
to do essentially the same thing but in a quarter-plane way.  In addition,
Picture mode defines @kbd{C-c} as a prefix character for more commands that
are specific to Picture mode.

  One of these commands, @kbd{C-c C-c}, is pretty important.  Often a
picture is part of a larger file that is usually edited in some other major
mode.  @kbd{M-x edit-picture} records the name of the previous major mode,
and then you can use the @kbd{C-c C-c} command (@code{Picture-mode-exit})
to restore that mode.  @kbd{C-c C-c} also deletes spaces from the ends of
lines, unless given a numeric argument.

  The commands used in Picture mode all work in other modes (provided the
@code{picture} library is loaded), but are not bound to keys except in
Picture mode.  Note that the descriptions below talk of moving ``one
column'' and so on, but all the picture mode commands handle numeric
arguments if their normal equivalents do.

@section{Basic Editing in Picture Mode}

@cfindex{Picture-forward-column}
@cfindex{Picture-backward-column}
@cfindex{Picture-move-down}
@cfindex{Picture-move-up}
  Most keys do the same thing in Picture mode that they usually do, but do
it in a quarter-plane style.  For example, @kbd{C-f} is rebound to run
@code{Picture-forward-column}, which is defined to move point one column to
the right, by inserting a space if necessary, so that the actual end of the
line makes no difference.  @kbd{C-b} is rebound to run
@code{Picture-backward-column}, which always moves point right one column,
converting a tab to multiple spaces if necessary.  @kbd{C-n} and @kbd{C-p}
are rebound to run @code{Picture-move-down} and @code{Picture-move-up},
which can either insert spaces or convert tabs as necessary to make sure
that point stays in exactly the same column.  @kbd{C-e} runs
@code{Picture-end-of-line}, which moves to after the last nonblank
character on the line.  There is no need to change @kbd{C-a}, as the choice
of screen model does not affect beginnings of lines.

@cfindex{Picture-newline}
  Insertion of text is adapted to the quarter-plane screen
model through the use of Overwrite mode (@pxref[Minor Modes]).
Self-inserting characters replace existing text, column by column, rather
than pushing existing text to the right.  @key(RET) runs
@code{Picture-newline}, which just moves to the beginning of the following
line so that new text will replace that line.

@cfindex{Picture-backward-clear-column}
@cfindex{Picture-clear-column}
@cfindex{Picture-clear-line}
  Deletion and killing of text are replaced with erasure.  @key(DEL)
runs @code{Picture-backward-clear-column}, and replaces the preceding
character with a space rather than removing it.  @kbd{C-d},
@code{Picture-clear-column}, does the same thing in a forward direction.
@kbd{C-k} runs @code{Picture-clear-line}; it really kills the contents of
lines, but does not ever remove the newlines from the buffer.

@cfindex{Picture-open-line}
  To do actual insertion, you must use special commands.  @kbd{C-o}
(@code{Picture-open-line}) still creates a blank line, but does so after
the current line; it never splits a line.  @kbd{C-M-o}, @code{split-line},
makes sense in Picture mode, so it is not changed.  @kbd{C-j}
(@code{Picture-duplicate-line}) inserts below the current line another line
with the same contents.

@kindex{C-c C-d}
@cfindex{delete-char}
  Real deletion can be done with @kbd{C-w}, or with @kbd{C-c C-d} (which is
defined as @code{delete-char}, as @kbd(C-d) is in other modes), or with one
of the picture rectangle commands (@pxref[Picture Rectangle]).

@section{Controlling Motion after Insert}

@cfindex{Picture-movement-up}
@cfindex{Picture-movement-down}
@cfindex{Picture-movement-left}
@cfindex{Picture-movement-right}
@cfindex{Picture-movement-nw}
@cfindex{Picture-movement-ne}
@cfindex{Picture-movement-sw}
@cfindex{Picture-movement-se}
@kindex{M-`}
@kindex{M-'}
@kindex{M--}
@kindex{M-=}
@kindex{C-c `}
@kindex{C-c '}
@kindex{C-c /}
@kindex{C-c Backslash}
  Since ``self-inserting'' characters in Picture mode just overwrite and
move point, there is no essential restriction on how point should be moved.
Normally point moves right, but you can specify any of the eight orthogonal
or diagonal directions for motion after a ``self-inserting'' character.
This is useful for drawing lines in the buffer.

@table 7
@item M-`
Move left after insertion (@code{Picture-movement-left}).
@item M-'
Move right after insertion (@code{Picture-movement-right}).
@item M--
Move up after insertion (@code{Picture-movement-up}).
@item M-=
Move down after insertion (@code{Picture-movement-down}).
@item C-c `
Move up and left (``northwest'') after insertion (@code{Picture-movement-nw}).
@item C-c '
Move up and right (``northeast'') after insertion
(@code{Picture-movement-ne}).
@item C-c /
Move down and left (``southwest'') after insertion (@code{Picture-movement-sw}).
@item C-c \
Move down and right (``southeast) after insertion (@code{Picture-movement-se}).
@end table

@kindex{C-c C-f}
@kindex{C-c C-b}
@cfindex{Picture-motion}
@cfindex{Picture-motion-reverse}
  Two motion commands move based on the current Picture insertion
direction.  @kbd{C-c C-f} (@code{Picture-motion}) moves in the same
direction as motion after ``insertion'' currently does, while @kbd{C-c C-b}
(@code{Picture-motion-reverse}) moves in the opposite direction.

@section{Picture Mode Tabs}
@setref Picture Rectangle

@kindex{M-TAB}
@cfindex{Picture-tab-search}
@vindex{picture-tab-chars}
  Two kinds of tab-like action are provided in Picture mode.
Context-based tabbing is done with @kbd{M-@key(TAB)}
(@code{Picture-tab-search}).  With no argument, it moves to a point
underneath the next ``interesting'' character that follows whitespace in
the previous nonblank line.  ``Next'' here means ``appearing at a
horizontal position greater than the one point starts out at''.  With an
argument, as in @kbd{C-u M-@key(TAB)}, this command moves to the next such
interesting character in the current line.  @kbd{M-@key(TAB)} does not
change the text; it only moves point.  ``Interesting'' characters are
defined by the variable @code{picture-tab-chars}, which contains a string
whose characters are all considered interesting.  Its default value is
@code{"!-~"}.

@cfindex{Picture-tab}
  @key(TAB) itself runs @code{Picture-tab}, which operates based on the
current tab stop settings; it is the Picture mode equivalent of
@code{tab-to-tab-stop}.  Normally it just moves point, but with a numeric
argument it clears the text that it moves over.

@kindex{C-c TAB}
@cfindex{Picture-set-tab-stops}
  The context-based and tab-stop-based forms of tabbing are brought
together by the command @kbd{C-c @key(TAB)},
@code{Picture-set-tab-stops}.  This command sets the tab stops to the
positions which @kbd{M-@key(TAB)} would consider significant in the current
line.  The use of this command, together with @key(TAB), can get the effect
of context-based tabbing.  But @kbd{M-@key(TAB)} is more convenient in the
cases where it is sufficient.

@section{Picture Mode Rectangle Commands}
@cindex{rectangle}

  Picture mode defines commands for working on rectangular pieces of the
text in ways that fit with the quarter-plane model.  The standard rectangle
commands may also be useful (@pxref[Rectangles]).

@table 7
@item C-c C-k
Clear out the region-rectangle.  With argument, kill it.
@item C-c k @var[r]
Similar but save rectangle contents in register @var[r] first.
@item C-c C-y
Overwrite last killed rectangle into the buffer, with upper left corner at
point.  With argument, insert instead.
@item C-c y @var[r]
Similar, but take the rectangle from register @var[r].
@end table

@kindex{C-c C-k}
@kindex{C-c k}
@cfindex{Picture-clear-rectangle}
@cfindex{Picture-clear-rectangle-to-register}
  The commands @kbd{C-c C-k} (@code{Picture-clear-rectangle}) and @kbd{C-c
k} (@code{Picture-clear-rectangle-to-register}) differ from the standard
rectangle commands in that they normally clear the rectangle instead of
deleting it; this is analogous with the way @kbd{C-d} is changed in Picture
mode.

  However, deletion of rectangles can be useful in Picture mode, so these
commands delete the rectangle if given a numeric argument.

@kindex{C-c C-y}
@kindex{C-c y}
@cfindex{Picture-yank-rectangle}
@cfindex{Picture-yank-rectangle-from-register}
  The Picture mode commands for yanking rectangles differ from the standard
ones in overwriting instead of inserting.  This is the same way that
Picture mode insertion of other text is different from other modes.
@kbd{C-c C-y} (@code{Picture-yank-rectangle}) inserts (by overwriting) the
rectangle that was most recently killed, while @kbd{C-c y}
(@code{Picture-yank-rectangle-from-register}) does likewise for the
rectangle found in a specified register.

@chapter{Miscellaneous Commands}

  This chapter contains several brief topics that do not fit anywhere else.

@section{Recursive Editing Levels}
@setref Recursive Edit
@cindex{recursive edit}

  A @dfn{recursive edit} is a situation in which you are using Emacs
commands to perform arbitrary editing while in the middle of another Emacs
command.  For example, when you type @kbd{C-r} inside of a
@code{query-replace}, you enter a recursive edit in which you can change
the current buffer.

@kindex{C-c}
@cfindex{exit-recursive-edit}
@cindex{exiting}
  @dfn{Exiting} the recursive edit means returning to the unfinished
command, which continues execution.  For example, exiting the recursive
edit requested by @kbd{C-r} in @code{query-replace} causes query replacing
to resume.  Exiting is done with @kbd{C-M-c} (@code{exit-recursive-edit}).
In most modes, @kbd{C-c} also runs this command.

@kindex{C-]}
@cfindex{abort-recursive-edit}
  You can also @dfn{abort} the recursive edit.  This is like exiting, but
the unfinished command is immediately aborted.  Use the command @kbd{C-]}
(@code{abort-recursive-edit}) for this.  @xref[Quitting].

  The mode line shows you when you are in a recursive edit, by displaying
square brackets around the parentheses that always surround the major and
minor mode names.  Every window's mode line shows this, in the same way,
since being in a recursive edit is true regardless of what buffer is
selected.

@cfindex{top-level}
  It is possible to be in recursive edits within recursive edits.  For
example, after typing @kbd{C-r} in a @code{query-replace}, you might type a
command that entered the debugger.  In such circumstances, two or more sets
of square brackets appear in the mode line.  Exiting the inner recursive
edit (such as, with the debugger @kbd{c} command) would resume the command
where it called the debugger.  After the end of this command, you would be
able to exit the first recursive edit.  Aborting also gets out of only one
level of recursive edit; it returns immediately to the command level of the
previous recursive edit.  So you could immediately abort that one too.

  Alternatively, the command @kbd{M-x top-level} aborts all levels of
recursive edits, returning immediately to the top level command reader.

  The text being edited inside the recursive edit need not be the
same text that you were editing at top level.  It depends on what
the recursive edit is for.  If the command that invokes the recursive edit
selects a different buffer first, that is the buffer you will edit
recursively.  In any case, you can switch buffers within the recursive
edit in the normal manner (as long as the buffer-switching keys have
not been rebound).  You could probably do all the rest of your editing
inside the recursive edit, visiting files and all.  But this could have
surprising effects (such as stack overflow) from time to time.  So
remember to exit or abory the recursive edit when you no longer need it.

  In general, GNU Emacs tries to avoid using recursive edits.  It is
usually preferable to allow the user to switch among the possible editing
modes in any order he likes.  With recursive edits, the only way to get to
another state is to go ``back'' to the state that the recursive edit was
invoked from.

@section{Mail}
@setref Mail
@cindex{mail}
@cindex{message}

@cfindex{rmail}
@cfindex{mh-rmail}
@cfindex{rnews}
  You can read your mail with Emacs in either of two ways, but they are not
yet fully documented.  @kbd{M-x rmail} invokes a mail-reading program that
runs entirely within Emacs.  @kbd{M-x mh-rmail} invokes an interface from
Emacs to the @code{mh} mail-reading program.  Also, @kbd{M-x rnews} runs a
program much like Rmail but intended for reading Usenet news.  For more
information, read the documentation of these functions using @kbd{C-h f}.

  To send a message in Emacs, you start by typing a command (@kbd{C-x m}) to select and
initialize the @code{*mail*} buffer.  Then you edit the text and headers of
the message in this buffer, and type another command (@kbd{C-c C-c}) to
send the message.

@table 7
@item C-x m
Begin composing a message to send.
@item C-c C-s
Send the message, and leave the @code{*mail*} buffer selected.
@item C-c C-c
Send the message and select some other buffer.  All the key sequences
starting with @kbd{C-c} that are documented here are defined this way only
in Mail mode.
@item C-c t
Move to the @samp{To} header field, creating one if there is none.
@item C-c s
Move to the @samp{Subject} header field, creating one if there is none.
@item C-c c
Move to the @samp{CC} header field, creating one if there is none.
@item C-c w
Insert the file @code{~/.signature} at the end of the message text.
@item C-c y
Yank the selected message from Rmail.  This command does nothing unless
your command to start sending a message was issued with Rmail.
@end table

@kindex{C-x m}
@cfindex{mail}
  The command @kbd{C-x m} (@code{mail}) selects a buffer named @code{*mail*} and
initializes it with the skeleton of an outgoing message.  @kbd{C-x 4 m}
(@code{mail-other-window}) does likewise but selects the @code{*mail*}
buffer in a different window, leaving the previous current buffer visible.

  The line in the buffer that says
@example
--Text follows this line--
@end example
@nopara
is a special delimiter.  Whatever follows it is the text of the message;
the headers precede it.  The delimiter line itself does not appear in the
message actually sent.

  Header fields you can use include @samp{To}, @samp{CC} and
@samp{Subject}.  @samp{To} specifies the main recipients of the message.
@samp{CC} specifies additional recipients; both kinds of recipients receive
the message just the same, but the people who see their names in the
@samp{CC} field know that they are simply being shown what is addressed to
the people in the @samp{To} field.  The @samp{Subject} field contains a
single line giving the topic or purpose of the message.  The header fields
look like this:
@example
To: rms@@mc
CC: mly@@mc, rg@@oz
Subject: The Emacs Manual
--Text follows this line--
@end example
@nopara
@samp{Subject:} can be abbreviated @samp{S:}.

  The major mode used in the @code{*mail*} buffer is Mail mode, which is
much like Text mode except that the character @kbd{C-c} is redefined to be
a prefix character, instead of its usual meaning of
@code{exit-recursive-edit}.  The commands that begin with @kbd{C-c} in Mail
mode all have to do specifically with editing or sending the message.

@kindex{C-c C-s}
@kindex{C-c C-c}
@cfindex{mail-send}
@cfindex{mail-send-and-exit}
  There are two ways to send the message.  @kbd{C-c C-s} (@code{mail-send})
sends the message and marks the @code{*mail*} buffer unmodified, but leaves
that buffer selected so that you can modify the message (perhaps with new
recipients) and send it again.  @kbd{C-c C-c} (@code{mail-send-and-exit})
sends and then deletes the window (if there is another window) or switches
to another buffer.  It puts the @code{*mail*} buffer at the lowest priority
for automatic reselection, since you are finished with using it.  This is
the usual way to send the message.

@kindex{C-c t}
@cfindex{mail-to}
@kindex{C-c s}
@cfindex{mail-subject}
@kindex{C-c c}
@cfindex{mail-cc}
  Mail mode provides some other special commands that are useful for
editing the headers and text of the message before you send it.  There are
four commands defined to move point to particular header fields: @kbd{C-c
t} (@code{mail-to}) to move to the @samp{To} field, @kbd{C-c s}
(@code{mail-subject}) for the @samp{Subject} field, and @kbd{C-c c}
(@code{mail-cc}) for the @samp{CC} field.

@kindex{C-c w}
@cfindex{mail-signature}
  @kbd{C-c w} (@code{mail-signature}) adds a standard piece text at the end of the
message to say more about who you are.  The text comes from the file
@code{.signature} in your home directory.

@kindex{C-c y}
@cfindex{mail-yank-original}
  When mail sending is invoked from the Rmail mail reader using an Rmail
command, @kbd{C-c y} can be used inside the @code{*mail*} buffer to insert
the text of the message you are replying to.  Normally it indents each line
of that message four spaces and eliminates most header fields.  A numeric
argument specifies the number of spaces to indent.  An argument of just
@kbd{C-u} says not to indent at all and not to eliminate anything.
@kbd{C-c y} always uses the current message from the @code{RMAIL} buffer,
so you can insert several old messages by selecting one in @code{RMAIL},
switching to @code{*mail*} and yanking it, then switching back to
@code{RMAIL} to select another.

  Because the mail composition buffer is an ordinary Emacs buffer, you can
switch to other buffers while in the middle of composing mail, and switch
back later (or never).  If you use the @kbd{C-x m} command again when you
have been composing another message but have not sent it, you are asked to
confirm before the old message is erased.  If you answer @kbd{n}, the
@code{*mail*} buffer is left selected with its old contents, so you can
finish the old message and send it.  @kbd{C-u C-x m} is another way to do
this.  Sending the message marks the @code{*mail*} buffer ``unmodified'',
which prevents confirmation when @kbd{C-x m} is next used.

@node[Narrowing, Pages, Windows, Top]

@chapter[Narrowing]
@setref Narrowing
@cindex{widening}
@cindex{restriction}
@cindex{narrowing}

  @dfn{Narrowing} means focusing in on some portion of the buffer, making
the rest temporarily invisible and inaccessible.  Cancelling the narrowing,
and making the entire buffer once again visible, is called @dfn{widening}.
The amount of narrowing in effect in a buffer at any time is called the
buffer's @dfn{restriction}.

@c WideCommands
@table 3
@item C-x n
Narrow down to between point and mark.
@item C-x w
Widen to make the entire buffer visible again.
@end table

  When you have narrowed down to a part of the buffer, that part appears to
be all there is.  You can't see the rest, you can't move into it (motion
commands won't go outside the visible part), you can't change it in any
way.  However, it is not gone, and if you save the file all the invisible
text will be saved.  In addition to sometimes making it easier to
concentrate on a single subroutine or paragraph by eliminating clutter,
narrowing can be used to restrict the range of operation of a replace
command or repeating keyboard macro.  The word @samp{Narrow} appears in the
mode line whenever narrowing is in effect.

@kindex{C-x n}
@cfindex{narrow-to-region}
  The primary narrowing command is @kbd{C-x n} (@code{narrow-to-region}).
It sets the current buffer's restrictions so that the text in the current
region remains visible but all text before the region or after the region
is invisible.  Point and mark do not change.

  Because narrowing can easily confuse users who do not understand it,
@code{narrow-to-region} is normally a disabled command.  Attempting to use
this command asks for confirmation and gives you the option of enabling it;
once you enable the command, confirmation will no longer be required for
it.  @xref[Disabling].

@kindex{C-x w}
@cfindex{widen}
  The way to undo narrowing is to widen with @kbd{C-x w} (@code{widen}).
This makes all text in the buffer accessible again.

  You can get information on what part of the buffer you are narrowed
down to using the @code{C-x =} command.  @xref[Position Info].

@node[Shell]
@section{Running Shell Commands from Emacs}
@setref Shell
@cindex{subshell}
@cindex{shell commands}

  Emacs has commands for passing single command lines to inferior shell
processes; it can also run a shell interactively with input and output to
an Emacs buffer @code{*shell*}.

@table 7
@item M-!
Run a specified shell command line and display the output.
@item M-|
Run a specified shell command line with region contents as input;
optionally replace the region with the output.
@item M-x shell
Run a subshell with input and output through an Emacs buffer.
You can then give commands interactively.
@end table

@subsection{Single Shell Commands}

@kindex{M-!}
@cfindex{shell-command}
  @kbd{M-!} (@code{shell-command}) reads a line of text using the
minibuffer and creates an inferior shell to execute the line as a command.
Standard input from the command comes from the null device.
If the shell command produces any output, the output goes into an Emacs
buffer named @code{*Shell Command Output*}, which is displayed in another
window but not selected.  A numeric argument, a in @kbd{M-1 M-!}, directs
this command to insert any output into the current buffer.  In that case,
point is left before the output and the mark is set after the output.

@kindex{M-|}
@cfindex{shell-command-on-region}
  @kbd{M-|} (@code{shell-command-on-region}) is like @kbd{M-!} but passes
the contents of the region as input to the shell command, instead of no
input.  If a numeric argument is used, meaning insert output in the current
buffer, then the old region is deleted first and the output replaces it as
the contents of the region.

@vindex{shell-file-name}
@cindex{environment}
  Both @kbd{M-!} and @kbd{M-|} use @code{shell-file-name} to specify the
shell to use.  This variable is initialized based on your @code{SHELL}
environment variable when Emacs is started.  If the file name does not
specify a directory, the directories in the list @code{exec-path} are
searched; this list is initialized based on the environment variable
@code{PATH} when Emacs is started.  Your @code{.emacs} file can override
either or both of these default initializations.

  With @kbd{M-!} and @kbd{M-|}, Emacs has to wait until the shell command
completes.  You can quit with @kbd{C-g}; that terminates the shell command.

@subsection{Interactive Inferior Shell}

@cfindex{M-x shell}
  To run a subshell interactively, putting its typescript in an Emacs
buffer, use @kbd{M-x shell}.  This creates (or reuses) a buffer named
@code{*shell*} and runs a subshell with input coming from and output going
to that buffer.  That is to say, any ``terminal output'' from the subshell
will go into the buffer, advancing point, and any ``terminal input'' for
the subshell comes from text in the buffer.  To give input to the subshell,
go to the end of the buffer and type the input, terminated by @key(RET).

  Emacs does not wait for the subshell to do anything.  You can switch
windows or buffers and edit them while the shell is waiting, or while it
is running a command.  Output from the subshell waits until Emacs has time
to process it; this happens whenever Emacs is waiting for keyboard input
or for time to elapse.

@vindex{explicit-shell-file-name}
  The file name used to load the subshell is the value of the variable
@code{explicit-shell-file-name}, if that is non-@code{nil}.  Otherwise, the
@code{ESHELL} environment variable is used, or the @code{SHELL} environment
variable if there is no @code{ESHELL}.  If no directory is specified, the
directories in the list @code{exec-path} are searched; see above.

  As soon as the subshell is started, it is sent as input the contents of
the file @code{~/.emacs_@var[shellname]}, where @var[shellname] is the
name of the file that the shell was loaded from.  For example, if you use
@code{csh}, the file sent to it is @code{~/.emacs_csh}.

@cindex{Shell mode}
  The shell buffer uses Shell mode, which attempts to imitate the usual
editing and job control characters present in shells that are not under
Emacs.  Here is a complete list of the special key bindings of Shell mode:

@kindex{RET}
@kindex{C-d}
@kindex{C-u}
@kindex{C-w}
@kindex{C-c}
@kindex{C-z}
@kindex{C-\}
@kindex{C-x C-k}
@kindex{C-x C-v}
@cfindex{send-shell-input}
@cfindex{delete-char-or-send-eof}
@cfindex{interrupt-shell-subjob}
@cfindex{stop-shell-subjob}
@cfindex{quit-shell-subjob}
@cfindex{kill-output-from-shell}
@cfindex{show-output-from-shell}
@table 7
@item @key(RET)
@vindex{shell-prompt-pattern}
At end of buffer send line as input; otherwise, copy current line to
end and send it (@code{send-shell-input}).  When a line is copied to
the end, any text at the beginning of the line that matches the variable
@code{shell-prompt-pattern} is left out; this variable's value should be a
regexp string that matches the prompts that you use in your subshell.
@item C-d
At end of buffer, send end-of-file as input; otherwise, delete a
character as usual (@code{delete-char-or-send-eof}).
@item C-u
Kill all text that has yet to be sent as input (@code{kill-shell-input}).
@item C-w
Kill a word before point (@code{backward-kill-word}).
@item C-c
Interrupt the shell or its current subjob if any
(@code{interrupt-shell-subjob}).
@item C-z
Stop the shell or its current subjob if any (@code{stop-shell-subjob}).
@item C-\
Send quit signal to the shell or its current subjob if any
(@code{quit-shell-subjob}).
@item C-x C-k
Delete last batch of output from shell (@code{kill-output-from-shell}).
@item C-x C-v
Scroll top of last batch of output to top of window (@code{show-output-from-shell}).
@end table

  @code{cd}, @code{pushd} and @code{popd} commands given to the inferior
shell are watched by Emacs so it can keep the @code{*shell*} buffer's
default directory the same as the shell's working directory.

@node[Hardcopy, ]

@section{Hardcopy Output}
@setref Hardcopy
@cindex{hardcopy}

@table 7
@item M-x print-buffer
Print hardcopy of current buffer using Unix command @code{lpr -p}.
This makes page headings containing the file name and page number.
@item M-x lpr-buffer
Print hardcopy of current buffer using Unix command @code{lpr}.
This makes no page headings.
@item M-x print-region
Like @code{print-buffer} but prints only the current region.
@item M-x lpr-region
Like @code{lpr-buffer} but prints only the current region.
@end table

@cfindex{print-buffer}
@cfindex{print-region}
@cfindex{lpr-buffer}
@cfindex{lpr-region}
@vindex{lpr-switches}
  All the hardcopy commands pass extra switches to the @code{lpr} program
based on the value of the variable @code{lpr-switches}.  Its value should
be a list of strings, each string a switch.

@node[Dissociation, ,Text Formatter, Text]

@section[Dissociated Press]
@setref Dissociated Press

@cfindex{dissociated-press}
  @kbd{M-x dissociated-press} is a command for scrambling a file of text
either word by word or character by character.  Starting from a buffer of
straight English, it produces extremely amusing output.  The input comes
from the current Emacs buffer.  Dissociated Press writes its output in a
buffer named @code{*Dissociation*}, and redisplays that buffer after every
couple of lines (approximately) to facilitate reading it.

  @code{dissociated-press} asks every so often whether to continue
operating.  Answer @kbd{n} to stop it.  You can also stop at any time by
typing @kbd{C-g}.  The dissociation output remains in the
@code{*Dissociation*} buffer for you to copy elsewhere if you wish.

@cindex{presidentagon}
  Dissociated Press operates by jumping at random from one point in the
buffer to another.  In order to produce plausible output rather than
gibberish, it insists on a certain amount of overlap between the end of one
run of consecutive words or characters and the start of the next.  That is,
if it has just printed out `president' and then decides to jump to a
different point in the file, it might spot the `ent' in `pentagon' and
continue from there, producing `presidentagon'.  Long sample texts produce
the best results.

@cindex{againformation}
  A positive argument to @kbd{M-x Dissociated Press} tells it to operate
character by character, and specifies the number of overlap characters.  A
negative argument tells it to operate word by word and specifies the number
of overlap words.  In this mode, whole words are treated as the elements to
be permuted, rather than characters.  No argument is equivalent to an
argument of two.  For your againformation, the output goes only into the
buffer @code{*Dissociation*}.  The buffer you start with is not changed.

@cindex{Markov chain}
@cindex{ignoriginal}
@cindex{techniquitous}
  Dissociated Press produces nearly the same results as a Markov chain
based on a frequency table constructed from the sample text.  It is,
however, an independent, ignoriginal invention.  Dissociated Press
techniquitously copies several consecutive characters from the sample
between random choices, whereas a Markov chain would choose randomly for
each word or character.  This makes for more plausible sounding results,
and runs faster.

@cindex{outragedy}
@cindex{buggestion}
@cindex{properbose}
  It is a mustatement that too much use of Dissociated Press can be a
developediment to your real work.  Sometimes to the point of outragedy.
And keep dissociwords out of your documentation, if you want it to be well
userenced and properbose.  Have fun.  Your buggestions are welcome.

@chapter[Customization]
@cindex{customization}
@setref Customization

  This chapter talks about various topics relevant to adapting the
behavior of Emacs in minor ways.

@node[Minor Modes, Libraries, Programs, Top]

@section[Minor Modes]
@setref Minor Modes
@cindex{minor modes}

@cindex{mode line}
  Minor modes are options which you can use or not.  For example, Auto Fill
mode is a minor mode in which @key(SPC) breaks lines between words as you type.
All the minor modes are independent of each other and of the selected major
mode.  Most minor modes say in the mode line when they are on; for example,
@samp{Fill} in the mode line means that Auto Fill mode is on.

  Append @code{-mode} to the name of a minor mode to get the name of a
command function that turns the mode on or off.  Thus, the command to
enable or disable Auto Fill mode is called @kbd{M-x auto-fill-mode}.  These
commands are usually invoked with @kbd{M-x}, but you can bind keys to them
if you wish.  With no argument, the function turns the mode on
if it was off and off if it was on.  This is known as @dfn[toggling].  A
positive argument always turns the mode on, and an explicit zero argument
or a negative argument always turns it off.

@cindex{Auto Fill mode}
  Auto Fill mode allows you to enter filled text without breaking lines
explicitly.  Emacs inserts newlines as necessary to prevent lines from
becoming too long.  @xref[Filling].

@cindex{Overwrite mode}
  Overwrite mode causes ordinary printing characters to replace existing
text instead of shoving it over.  It is good for editing pictures.  For
example, if the point is in front of the @samp{B} in @samp{FOOBAR}, then in
Overwrite mode typing a @kbd{G} changes it to @samp{FOOGAR}, instead of
making it @samp{FOOGBAR} as usual.

@cindex{Abbrev mode}
  Abbrev mode allows you to define abbreviations that automatically
expand as you type them.  For example, @samp{amd} might expand to
@samp{abbrev mode}.  @xref[Abbrevs], for full information.

@node[Variables, Syntax, Libraries]

@section[Variables]
@setref Variables
@cindex{variable}
@cindex{option}

  A @dfn{variable} is a Lisp symbol which has a value.  The symbol's name
is also called the name of the variable.  Variable names can contain any
characters, but conventionally they are chosen to be words separated by
hyphens.  A variable can have a documentation string which describes what
kind of value it should have and how the value will be used.

  Lisp allows any variable to have any kind of value, but most variables
that Emacs uses require a value of a certain type.  Often the value should
always be a string, or should always be a number.  Sometimes we say that a
certain feature is turned on if a variable is ``non-@code{nil},'' meaning
that if the variable's value is @code{nil}, the feature is off, but
the feature is on for @i[any] other value.  The conventional value to use
to turn on the feature---since you have to pick one particular value when
you set the variable---is @code{t}.

  Emacs uses many Lisp variables for internal recordkeeping, as any Lisp
program must, but the most interesting variables for you are the ones that
exist for the sake of customization.  Emacs does not (usually) change the
values of these variables; instead, you set the values, and thereby alter
and control the behavior of certain Emacs commands.  These variables are
called @dfn{options}.  Most options are documented in this manual, and
appear in the Variables Index.

  One example of a variable which is an option is @code{fill-column}, which
specifies the position of the right margin (as a number of characters from
the left margin) to be used by the fill commands (@pxref[Filling]).

@table 7
@item C-h v
@itemx M-x describe-variable
Print the value and documentation of a variable.
@item M-x set-variable
Change the value of a variable.
@item M-x make-local-variable
Make a variable have a local value in the current buffer.
@item M-x kill-local-variable
Make a variable use its global value in the current buffer.
@item M-x list-options
Display a buffer listing names, values and documentation of all options.
@item M-x edit-options
Change option values by editing a list of options.
@end table

@subsection{Examining and Setting Variables}

@kindex{C-h v}
@cfindex{describe-variable}
  To examine the value of a single variable, use @kbd{C-h v}
(@code{describe-variable}), which reads a variable name using the
minibuffer, with completion.  It prints both the value and the
documentation of the variable.

@example
C-h v fill-column @key(RET)
@end example
@nopara
prints something like
@example
fill-column's value is 75

Documentation:
*Column beyond which automatic line-wrapping should happen.
Separate value in each buffer.
@end example
@nopara
@cindex{option}
The star at the beginning of the documentation indicates that this variable
is an option.  Commands that operate on a single, specified variable are
not restricted to options; they allow any variable name.

@cfindex{list-options}
  @kbd{M-x list-options} displays a list of all Emacs option variables, in
an Emacs buffer named @code{*List Options*}.  Each option is shown with its
documentation and its current value.  Here is what a portion of it might
look like:
@example
;; exec-path:
	("." "/usr/local/bin" "/usr/ucb" "/bin" "/usr/bin" "/u2/emacs/etc")
*List of directories to search programs to run in subprocesses.
Each element is a string (directory name) or nil (try default directory).
;;
;; fill-column:
	75
*Column beyond which automatic line-wrapping should happen.
Separate value in each buffer.
;;
;; find-file-hook:
	nil
*If non-nil specifies a function to be called after a buffer
is found or reverted from a file.
The buffer's local variables (if any) will have been processed before the
function is called.
;;
@end example

@cfindex{edit-options}
  @kbd{M-x edit-options} goes one step farther and selects the @code{*List
Options*} buffer; this buffer uses the major mode Options mode, which
provides commands that allow you to point at an option and change its
value:

@table 7
@item s
Set the variable point is in or near to a new value read using the minibuffer.
@item x
Toggle the variable point is in or near: if the value was @code{nil}, it
becomes @code{t}; otherwise it becomes @code{nil}.
@item 1
Set the variable point is in or near to @code{t}.
@item 0
Set the variable point is in or near to @code{nil}.
@item n
@itemx p
Move to the next or previous variable.
@end table

@cfindex{set-variable}
  If you know which option you want to set, you set it without waiting for
@code{edit-options} to make the option list using @kbd{M-x set-variable}.
This reads the variable name with the minibuffer (with completion), and
then reads a Lisp expression for the new value using the minibuffer a
second time.  For example,
@example
M-x set-variable @key(RET) fill-column @key(RET) 75 @key(RET)
@end example
@nopara
sets @code{fill-column} to 75.

  If you want to set a variable a particular way each time you use Emacs,
you can use the Lisp function @code{setq} in your @code{.emacs} file.

@subsection{Local Variables}

@cindex{local variables}
  Any variable can be made @dfn{local} to a specific Emacs buffer.  This
means that its value in that buffer is independent of its value in other
buffers.  A few variables are always local in every buffer; aside from
this, major modes always make the variables they set local to the buffer.
This is why changing major modes in one buffer has no effect on other
buffers.  Every other Emacs variable has a @dfn{global} value which is in
effect in all buffers that have not made the variable local.

@cfindex{make-local-variable}
  @kbd{M-x make-local-variable} reads the name of a variable and makes it
local to the current buffer.  Further changes in this buffer will not
affect others, and further changes in the global value will not affect this
buffer.

@cfindex{kill-local-variable}
  @kbd{M-x kill-local-variable} reads the name of a variable and makes it
cease to be local to the current buffer.  The global value of the variable
henceforth is in effect in this buffer.  Setting the major mode kills all
the local variables of the buffer, except for those variables that are
always local to every buffer.

@subsection{Local Variables in Files}
@setref Local Variables List
@cindex{local variables in files}

  A file can contain a @dfn[local variables list], which specifies the values
to use for certain Emacs variables when that file is edited.  Visiting the
file checks for a local variables list and makes each variable in the list
local to the buffer in which the file is visited, with the value specified
in the file.

  A local variables list goes near the end of the file, in the last page.
(It is often best to put it on a page by itself.)  The local variables
list starts with a line containing the string @samp{Local Variables:}, and
ends with a line containing the string @samp{End:}.  In between come the
variable names and values, one set per line, as @samp{@var[variable]:
@var[value]}.  The @var[value]s are not evaluated; they are used literally.

  The line which starts the local variables list does not have to say just
@samp{Local Variables:}.  If there is other text before @samp{Local
Variables:}, that text is called the @dfn[prefix], and if there is other
text after, that is called the @dfn[suffix].  If these are present, each
entry in the local variables list should have the prefix before it and the
suffix after it.  This includes the @samp{End:} line.  The prefix and
suffix are included to disguise the local variables list as a comment so
that the compiler or text formatter will not be perplexed by it.  If you
do not need to disguise the local variables list as a comment in this way,
do not bother with a prefix or a suffix.

  Two ``variable'' names are special in a local variables list:
a value for the variable @code{mode} really sets the major mode,
and a value for the variable @code{eval} is simply evaluated as an
expression and the value is ignored.  These are not real variables;
setting such variables in any other context has no such effect.
If @code{mode} is used in a local variables list, it should be the first
entry in the list.

Here is an example of a local variables list:
@example
;;; Local Variables: ***
;;; mode:lisp ***
;;; comment-column:0 ***
;;; comment-start: ";;; "  ***
;;; comment-end:"***" ***
;;; End: ***
@end example

  Note that the prefix is @samp{;;; } and the suffix is @samp{ ***}.  Note also
that comments in the file begin with and end with the same strings.
Presumably the file contains code in a language which is like Lisp
(like it enough for Lisp mode to be useful) but in which comments start
and end in that way.  The prefix and suffix are used in the local
variables list to make the list appear as comments when the file is read
by the compiler or interpreter for that	language.

  The start of the local variables list must be no more than 3000
characters from the end of the file, and must be in the last page if the
file is divided into pages.  Otherwise, Emacs will not notice it is there.
The purpose of this is so that a stray @samp{Local Variables:} not in the
last page does not confuse Emacs, and so that visiting a long file that is
all one page and has no local variables list need not take the time to
search the whole file.

@node[Keyboard Macros, Minibuffer, Locals]

@section[Keyboard Macros]
@setref Keyboard Macros

@cindex{keyboard macros}
  A @dfn[keyboard macro] is a command defined by the user to abbreviate a
sequence of other commands.  For example, if you discover that you are
about to type @kbd{C-n C-d} forty times, you can speed your work by
defining a keyboard macro to do @kbd{C-n C-d} and calling it with a repeat
count of forty.

@c widecommands
@table 7
@item C-x (
Start defining a keyboard macro.
@item C-x )
End the definition of a keyboard macro.
@item C-x e
Execute the most recent keyboard macro.
@item C-u C-x (
Re-execute last keyboard macro, then add more commands to its definition.
@item C-x q
Ask for confirmation when the keyboard macro is executed.
@item M-x name-last-kbd-macro
Give a permanent name (for the duration of the session)
to the most recently defined keyboard macro.
@item M-x write-kbd-macro
Store the definition of a keyboard macro into a file.
@item M-x append-kbd-macro
Append the  definition of a keyboard macro to the end of a file.
@end table

  Keyboard macros differ from ordinary Emacs commands in that they are
written in the Emacs command language rather than in Lisp.  This makes it
easier for the novice to write them, and makes them more convenient as
temporary hacks.  However, the Emacs command language is not powerful
enough as a programming language to be useful for writing anything
intelligent or general.  For such things, Lisp must be used.

  You define a keyboard macro while executing the commands which are the
definition.  Put differently, as you are defining a keyboard macro, the
definition is being executed for the first time.  This way, you can see
what the effects of your commands are, so that you don't have to figure
them out in your head.  When you are finished, the keyboard macro is
defined and also has been, in effect, executed once.  You can then do the
whole thing over again by invoking the macro.

@subsection[Basic Use]

@kindex{C-x (}
@kindex{C-x )}
@kindex{C-x e}
@cfindex{start-kbd-macro}
@cfindex{end-kbd-macro}
@cfindex{call-last-kbd-macro}
  To start defining a keyboard macro, type the @kbd[C-x (] command
(@code{start-kbd-macro}).  From then on, your commands continue to be
executed, but also become part of the definition of the macro.  @samp{Def}
appears in the mode line to remind you of what is going on.  When you are
finished, the @kbd[C-x )] command (@code{end-kbd-macro}) terminates the
definition (without becoming part of it!).  For example
@example
C-x ( M-F foo C-x )
@end example
@nopara
defines a macro to move forward a word and then insert @samp{foo}.

  The macro thus defined can be invoked again with the @kbd{C-x e} command
(@code{call-last-kbd-macro}), which may be given a repeat count as a
numeric argument to execute the macro many times.  @kbd[C-x )] can also be
given a repeat count as an argument, in which case it repeats the macro
that many times right after defining it, but defining the macro counts as
the first repetition (since it is executed as you define it).  So, giving
@kbd[C-x )] an argument of 4 executes the macro immediately 3 additional
times.  An argument of zero to @kbd[C-x e] or @kbd[C-x )] means repeat the
macro indefinitely (until it gets an error, or you type @kbd{C-g}).

  If you wish to repeat an operation at regularly spaced places in the
text, define a macro and include as part of the macro the commands to
move to the next place you want to use it.  For example, if you want to
change each line, you should position point at the start of a line,
and define a macro to change that line and leave point at the start of
the next line.  Then repeating the macro will operate on successive lines.

  After you have terminated the definition of a keyboard macro, you can add
to the end of its definition by typing @kbd{C-U C-x (}.  This is
equivalent to plain @kbd{C-x (} followed by retyping the whole definition
so far.  As a consequence it re-executes the macro as previously defined.

@subsection{Naming and Installing Keyboard Macros}

@cfindex{name-last-kbd-macro}
  If you wish to save a keyboard macro for longer than until you define the
next one, you must give it a name or install it on a command sequence.  To
give the macro a name, use @kbd{M-x name-last-kbd-macro}.  This reads a
name as an argument using the minibuffer and defines that name to execute
the macro.  The macro name is a Lisp symbol, and defining it in this way
makes it valid for calling with @kbd{M-x} or for binding a key to it with
@code{global-set-key} (@pxref[Keymaps]).

@subsection[Executing Macros with Variations]

@kindex{C-x q}@cfindex{kbd-macro-query}
  Using @kbd{C-x q} (@code{kbd-macro-query}), you can get an effect similar
to that of @code{query-replace}, where the macro asks you each time around
whether to make a change.  When you are defining the macro, type @kbd{C-x
q} at the point where you want the query to occur.  During macro definition,
the @kbd{C-x q} does nothing, but when the macro is invoked the @kbd{C-x q}
reads a character from the terminal to decide whether to continue.

  The special answers are @key(SPC), @key(DEL), @kbd{C-d}, @kbd{C-l}
and @kbd{C-r}.  Any other character terminates execution of the keyboard
macro and is then read as a command.  @key(SPC) means to continue.
@key(DEL) means to skip the remainder of this repetition of the macro,
starting again from the beginning in the next repetition.  @kbd{C-d} means
to skip the remainder of this repetition and cancel further repetition.
@kbd(C-l) clears the screen and asks you again for a character to say what
to do.  @kbd{C-r} enters a recursive editing level, in which you can
perform editing which is not part of the macro.  When you exit the
recursive edit using @kbd{C-M-c}, you are asked again how to continue with
the keyboard macro.  If you type a @key(SPC) at this time, the rest of
the macro definition is executed.  It is up to you to leave point and the
text in a state such that the rest of the macro will do what you want.

  @kbd{C-u C-x q}, @kbd{C-x q} with a numeric argument, performs a
different function.  It enters a recursive edit reading input from the
keyboard, both when you type it during the definition of the macro, and
when it is executed from the macro.  During definition, the editing you do
inside the recursive edit does not become part of the macro.  During macro
execution, the recursive edit gives you a chance to do some particularized
editing.  @xref[Recursive Edit].

@node[Keymaps]

@section[Customizing Key Bindings]
@setref Keymaps

  This section deals with the @dfn{keymaps} which define the bindings
between keys and functions, and say how you can customize these bindings.

@subsection[Commands and Functions]
@cindex{command}
@cindex{function}
@cindex{command name}

  A command is a Lisp function whose definition says how to call it
interactively (from the editor command loop).  Like every Lisp function, a
command has a function name, a Lisp symbol whose name usually consists of
lower case letters and dashes.

@subsection[Keymaps]
@cindex{keymap}

@cindex{global keymap}
@vindex{global-map}
  The bindings between characters and command functions are recorded in
data structures called @dfn{keymaps}.  Emacs has many of these.  One, the
@dfn{global} keymap, defines the meanings of the single keys that are
defined regardless of major mode.  It is the value of the variable
@code{global-map}.

@cindex{local keymap}
@vindex{c-mode-map}
@vindex{lisp-mode-map}
  Each major mode has another keymap, its @dfn{local keymap}, which
contains overriding definitions for the single keys that are to be
redefined in that mode.  Each buffer records which local keymap is
installed for it at any time, and the current buffer's local keymap is the
only one that directly affects command execution.  The local keymaps
for Lisp mode, C mode, and other major modes exist always even when not in
use.  They are the values of the variables @code{lisp-mode-map},
@code{c-mode-map}, and so on.  For major modes less often used, the local
keymap is often constructed only when the mode is used for the first time
in a session.  This is to save space.

@vindex{ctl-x-map}
@vindex{help-map}
@vindex{esc-map}
  Finally, each prefix key has a keymap which defines the key sequences
that start with it.  For example, @code{ctl-x-map} is the keymap used for
characters following a @kbd{C-x}, and @code{help-map} is the keymap used
for characters following a @kbd{C-h}.  @code{esc-map} is the keymap used
for characters following @key(ESC), and therefore for all Meta characters
(see below).  In fact, the definition of a prefix key is just the keymap to
use for looking up the following character.  Actually, the definition
is usually a Lisp symbol whose function definition is the following
character keymap.  The effect is the same, but it provides a command name
for the prefix key that can be used as a description of what the prefix
key is for.  Thus, the binding of @kbd{C-x} is the symbol
@code{Ctl-X-Prefix}, whose function definition is the keymap for @kbd{C-x}
commands, the value of @code{ctl-x-map}.

  Prefix key definitions of this sort can appear in either the global map
or a local map.  The definitions of @kbd{C-x}, @kbd{C-h} and @key(ESC)
as prefix keys appear in the global map, so these prefix keys are always
available.  Modes such as Mail mode and Picture mode that make @kbd{C-c}
into a prefix character do so by putting prefix definitions into their
local maps.  A mode can also put a prefix definition of a global prefix
character such as @kbd{C-x} into its local map.  This is how major modes
override the definitions of certain keys that start with @kbd{C-x}.  When
both the global and local definitions of a key are other keymaps, the next
character is looked up in both keymaps, with the local definition
overriding the global one as usual.  So, the character after the @kbd{C-x}
is looked up in both the major mode's own keymap for redefined @kbd{C-x}
commands and in @code{ctl-x-map}.  If the major mode's own keymap for
@kbd{C-x} commands contains @code{nil}, the definition from the global
keymap for @kbd{C-x} commands is used.

@cindex{sparse keymap}
  A keymap is actually a Lisp object.  The simplest form of keymap is a
Lisp vector of length 128.  The binding for a character in such a keymap is
found by indexing into the vector with the character as an index.  A keymap
can also be a Lisp list whose car is the symbol @code{keymap} and whose
remaining elements are pairs of the form @code{(@var[char] .
@var[binding])}.  Such lists are called @dfn{sparse keymaps} because they
are used when most of the characters' entries will be @code{nil}.  Sparse
keymaps are used mainly for prefix characters.

  Keymaps are only of length 128, so what about Meta characters, whose
codes are from 128 to 255?  A key that contains a Meta character actually
represents it as a sequence of two characters, the first of which is
@key(ESC).  So the key @kbd{M-a} is really represented as @kbd{@key(ESC)
a}, and its binding is found at the slot for @samp{a} in @code{esc-map}.

@subsection[Changing Key Bindings Interactively]

  The way to redefine an Emacs command is to change an entry in a keymap.
You can change the global keymap, in which case the change is effective in
all major modes (except those that have their own overriding local
definitions for the same key).  Or you can change the current buffer's
local map, which affects all buffers using the same major mode.
@cfindex{global-set-key}
@cfindex{local-set-key}

@table 7
@item M-x global-set-key @key(RET) @var[key] @var[cmd] @key(RET)
Defines @var[key] globally to run @var[cmd].
@item M-x local-set-key @key(RET) @var[key] @var[cmd] @key(RET)
Defines @var[key] locally (in the major mode now in effect) to run
@var[cmd].
@end table

  For example,
@example
M-x global-set-key @key(RET) C-f next-line @key(RET)
@end example
@nopara
would redefine @kbd{C-f} to move down a line.  The fact that @var[cmd] is
read second makes it serve as a kind of confirmation for @var[key].

  There is no way to specify a particular prefix keymap as the one to
redefine in, but that is not necessary, as you can include prefixes in
@var[key].  @var[key] is read by reading characters one by one until they
amount to a complete key (that is, not a prefix key).  Thus, if you type
@kbd{C-f} for @var[key], that's the end; the minibuffer is entered
immediately to read @var[cmd].  But if you type @kbd{C-x}, another
character is read; if that is @kbd{4}, another character is read, and so
on.  For example,
@example
M-x global-set-key @key(RET) C-x 4 $ dictionary-other-window @key(RET)
@end example
@nopara
would redefine @kbd{C-x 4 $} to run the (fictitious) command
@code{dictionary-other-window}.

@subsection{Disabling Commands}
@setref Disabling
@cindex{disabled command}

  Disabling a command marks the command as requiring confirmation before it
can be executed.  The purpose of disabling a command is to prevent
beginning users from executing it by accident and being confused.

  The direct mechanism for disabling a command is to have a non-@code{nil}
@code{disabled} property on the Lisp symbol for the command.  These
properties are normally set up by the user's @code{.emacs} file with
Lisp expressions such as
@example
(put 'delete-region 'disabled t)
@end example

@cfindex{disable-command}
@cfindex{enable-command}
  You can make a command disabled either by editing the @code{.emacs} file
directly or with the command @kbd{M-x disable-command}, which edits the
@code{.emacs} file for you.  To cancel the disablement of a command, use
@kbd{M-x enable-command}.

  Attempting to invoke a disabled command interactively in Emacs causes the
display of a window containing the command's name, its documentation, and
some instructions on what to do immediately; then Emacs asks for input
saying whether to execute the command as requested, enable it and execute,
or cancel it.  If you decide to enable the command, you is asked
whether to do this permanently (that is, change your @code{.emacs} file) or
just for the current session.

  Whether a command is disabled is independent of what key is used to
invoke it; it also applies if the command is invoked using @kbd{M-x}.
Disabling a command has no effect on calling it as a function from Lisp
programs.

@node[Syntax, FS Flags, Variables, Top]

@section[The Syntax Table]
@setref Syntax
@cindex{syntax table}

  All the Emacs commands which parse words or balance parentheses are
controlled by the @dfn[syntax table].  The syntax table says which
characters are opening delimiters, which are parts of words, which are
string quotes, and so on.  Actually, each major mode has its own syntax
table (though sometimes related major modes use the same one) which it
installs in each buffer that uses that major mode.  The syntax table
installed in the current buffer is the one that all commands use.  So we
will call it ``the syntax table''.  A syntax table is a Lisp object, a
vector of length 256 whose elements are numbers.

  The syntax table entry for a character holds six pieces of information:
@itemize @bullet
@item
The syntactic class of the character, represented as a small integer.
@item
The matching delimiter, for delimiter characters only.
The matching delimiter of @samp{(} is @samp{)}, and vice versa.
@item
A flag saying whether the character is the first character of a
two-character comment starting sequence.
@item
A flag saying whether the character is the second character of a
two-character comment starting sequence.
@item
A flag saying whether the character is the first character of a
two-character comment ending sequence.
@item
A flag saying whether the character is the second character of a
two-character comment ending sequence.
@end itemize

  The syntactic classes are stored internally as small integers, but are
usually described to or by the user with characters.  For example,
@samp{(} is used to specify the syntactic class of opening delimiters.
Here is a table of syntactic classes, with the characters that specify
them.
@table 7
@item @key(SPC)
The class of whitespace characters.
@item w
The class of word-constituent characters.
@item _
The class of characters that are part of symbol names but not words.
This class is represented by @samp{_} because the character @samp{_}
has this class in both C and Lisp.
@item (
The class of opening delimiters. 
@item )
The class of closing delimiters. 
@item '
The class of expression-adhering characters.  These characters are part of
a symbol if found within or adjacent to one one, and are part of a
following expression if immediately preceding one, but are like whitespace
if surrounded by whitespace.
@item "
The class of string-quote characters.  They match each other in pairs, and
the characters within the pair all lose their syntactic significance except
for the @samp{\} and @samp{/} classes of escape characters, which can be
used to include a string-quote inside the string.
@item $
The class of self-matching delimiters.  This is intended for @TeX's
@samp{$}, but it has not really been well worked out what it should do.
If you don't like its current behavior, please complain.
@item /
The class of escape characters that always just deny the following
character its special syntactic significance.  The character after one of
these escapes is always treated as alphabetic.
@item \
The class of C-style escape characters.  In practice, these are treated
just like @samp{/}-class characters, because the extra possibilities for C
escapes (such as being followed by digits) have no effect on where the
containing expression ends.
@item <
The class of comment-starting characters.  Only single-character comment
starters (such as @samp{;} in Lisp mode) are represented this way.
@item >
The class of comment-ending characters.  Newline has this syntax in Lisp mode.
@end table

@vindex{parse-sexp-ignore-comments}
  The characters marked as part of two-character comment delimiters can
have other syntactic functions most of the time.  The comment-delimiter
significance overrides when the pair of characters occur together in the
proper order.  Only the list and sexp commands use the syntax table to find
comments; the commands specifically for comments have other variables that
tell them where to find comments.  And the list and sexp commands notice
comments only if @code{parse-sexp-ignore-comments} is non-@code{nil}.  This
variable is set to @code{nil} in modes where comment-terminator sequences
are liable to appear where there is no comment; for example, in Lisp mode
where the comment terminator is a newline but not every newline ends a
comment.

@cfindex{modify-syntax-entry}
  @kbd{M-x modify-syntax-entry} is the command to change a character's
syntax in the current syntax table.  It can be used interactively, and is
also the means used by major modes to initialize their own syntax tables.
Its first argument is the character to change.  The second argument is a
string that specifies the new syntax:
@enumerate
@item
The first character in the string specifies the syntactic class.  It is one
of the characters in the previous table.
@item
The second character is the matching delimiter.  For a
character that is not an opening or closing delimiter, this should be a
space, or may be omitted if no following characters are needed.
@item
The remaining characters are flags.  The flag characters allowed are
@table 3
@item 1
Mark this character as the first of a two-character comment-start sequence.
@item 2
Mark this character as the second of a two-character comment-start sequence.
@item 3
Mark this character as the first of a two-character comment-end sequence.
@item 4
Mark this character as the second of a two-character comment-end sequence.
@end table
@end enumerate

@kindex{C-h s}
@cfindex{describe-syntax}
  A description of the contents of the current syntax table can be
displayed with @kbd{C-h s} (@code{describe-syntax}).  The description of
each character includes both the string you would have to give to
@code{modify-syntax-entry} to set up that character's current syntax, and
some English to explain that string if necessary.

@iftex
@chapter[Correcting Mistakes and Emacs Problems]

  If you type an Emacs command you did not intend, the results are often
mysterious.  This chapter tells what you can do to cancel your mistake or
recover from a mysterious situation.  Emacs bugs and system crashes are
also considered.
@end iftex

@node[Quitting, Lossage, Minibuffer, Top]

@section[Quitting and Aborting]
@setref Quitting
@cindex{quitting}

@table 7
@item C-g
Quit.  Cancel running or partially typed command.
@item C-]
Abort recursive editing level and cancel the command which invoked it.
@item M-x top-level
Abort all recursive editing levels that are currently executing.
@end table

  There are three ways of cancelling commands which are not finished
executing: @dfn[quitting] with @kbd{C-g}, and @dfn[aborting] with
@kbd{C-]} or @kbd{M-x top-level}.  Quitting is cancelling a partially typed
command or one which is already running.  Aborting is getting out of a
recursive editing level and cancelling the command that invoked the
recursive edit.

@cindex{quitting}
@cindex{C-g}
  Quitting with @kbd{C-g} is used for getting rid of a partially typed command,
or a numeric argument that you don't want.  It also stops a running
command in the middle in a relatively safe way, so you can use it if
you accidentally give a command which takes a long time.  In
particular, it is safe to quit out of killing; either your text will
@var[all] still be there, or it will @var[all] be in the kill ring
(or maybe both).  Quitting an incremental search does special things
documented under searching; in general, it may take two successive
@kbd{C-g} characters to get out of a search.  @kbd{C-g} works by setting
the variable @code{quit-flag} to @code{t} the instant @kbd{C-g} is typed;
Emacs Lisp checks this variable frequently and quits if it is
non-@code{nil}.  @kbd{C-g} is only actually executed as a command if it is
typed while Emacs is waiting for input.

  If you quit twice in a row before the first @kbd{C-g} is recognized,
you activate the ``emergency escape'' feature and return to the shell.
@xref[Emergency Escape].

@cindex{recursive editing level}
@cindex{aborting}
@cfindex{abort-recursive-edit}
@kindex{C-]}
  Aborting with @kbd{C-]} (@code{abort-recursive-edit}) is used to get out
of a recursive editing level and cancel the command which invoked it.
Quitting with @kbd{C-g} does not do this, and could not do this, because it
is used to cancel a partially typed command @i[within] the recursive
editing level.  Both operations are useful.  For example, if you are in the
Emacs debugger (@pxref[Lisp Debug]) and have typed @kbd{C-u 8} to enter a
numeric argument, you can cancel that argument with @kbd{C-g} and remain in
the debugger.

@cfindex{top-level}
  The command @kbd{M-x top-level} is equivalent to ``enough'' @kbd{C-]} commands to get you out of all the
levels of subsystems and recursive edits that you are in.  @kbd{C-]} gets
you out one level at a time, but @kbd{M-x top-level} goes out all levels at
once.  Both @kbd{C-]} and @kbd{M-x top-level} are like all other commands, and
unlike @kbd{C-g}, in that they are effective only when Emacs is ready for a
command.  @kbd{C-]} is an ordinary key and has its meaning only because of
its binding in the keymap.

@node[Lossage, Undo, Quitting, Top]

@section[Dealing with Emacs Trouble]
@setref Lossage

  This section describes various conditions which can cause Emacs not to
work, or cause it to display strange things, and how you can correct them.

@subsection[Recursive Editing Levels]

  Recursive editing levels are important and useful features of Emacs, but
they can seem like malfunctions to the user who does not understand them.

  If the mode line starts with a bracket @samp{[}, you have entered a
recursive editing level.  To get back to top level, type @kbd{M-x
top-level}.  @xref[Recursive Edit].

@subsection[Garbage on the Screen]

  If the data on the screen looks wrong, the first thing to do is see
whether the text is really wrong.  Type @kbd{C-l}, to redisplay
the entire screen.  If it appears correct after this, the problem was
entirely in the previous screen update.

  Display updating problems often result from an incorrect termcap entry
for the terminal you are using.  The file @code{etc/TERMS} gives the fixes
for known problems of this sort.  @code{INSTALL} contains general advice
for these problems in one of its sections.  Very likely there is simply insufficient padding for
certain display operations.  To investigate the possibility that you have
this sort of problem, try Emacs on another terminal made by a different
manufacturer.  If problems happen frequently on one kind of terminal but
not another kind, it is likely to be a bad termcap entry, though it could
also be due to a bug in Emacs that appears for terminals that have or that
lack specific features.

@subsection[Garbage in the Text]

  If @kbd{C-l} shows that the text is wrong, try undoing the changes to it
using @kbd{C-x u} until it gets back to a state you consider correct.
Also try @kbd{C-h l} to find out what command you typed to produce the
observed results.

@subsection[Spontaneous Entry to Incremental Search]

  If Emacs spontaneously displays @samp{I-search:} at the bottom of the
screen, it means that the terminal is sending @kbd{C-s} and @kbd{C-q}
according to the badly designed xon/xoff ``flow control'' protocol.  You
should try to prevent this by putting the terminal in a mode where it will
not use flow control or giving it enough padding that it will never send a
@kbd{C-s}.  If that cannot be done, you must tell Emacs to expect flow
control to be used, until you can get a properly designed terminal.

  Information on how to do these things can be found in the file
@code{INSTALL} in the Emacs distribution.

@subsection[Emergency Escape]
@setref Emergency Escape

  Because at times there have been bugs causing Emacs to loop without
checking @code{quit-flag}, a special feature causes Emacs to be suspended
immediately if you type a second @kbd{C-g} while the flag is already set.
So you can always get out of GNU Emacs.  Normally Emacs recognizes and
clears @code{quit-flag} (and quits!)  quickly enough to prevent this from
happening.

  When you resume Emacs after a suspension caused by multiple @kbd{C-g}, it
asks two questions before going back to what it had been doing:
@example
Checkpoint?
Dump core?
@end example
@nopara
Answer each one with @kbd{y} or @kbd{n} followed by @key(RET).

  Saying @kbd{y} to @samp{Checkpoint?} causes immediate auto-saving of all
modified buffers in which auto-saving is enabled.

  Saying @kbd{y} to @samp{Dump core?} causes an illegal instruction to be
executed, dumping core.  This is to enable a wizard to figure out why Emacs
was failing to quit in the first place.  Execution does not really continue
after a core dump.  If you answer @kbd{n}, execution does continue.  With
luck, GNU Emacs will ultimately check @code{quit-flag} and quit normally.
If not, and you type another @kbd{C-g}, it is suspended again.

  If Emacs is not really hung, just slow, you may invoke the double
@kbd{C-g} feature without really meaning to.  Then just resume and answer
@kbd{n} to both questions, and you will arrive at your former state.
Presumably the quit you requested will happen soon.

@node[Bugs, PICTURE, Journals, Top]

@section[Reporting Bugs]
@setref Bugs

@cindex{bugs}
  Sometimes you will encounter a bug in Emacs.  To get it fixed, you must
report it.  It is your duty to do so; but you must know when to do so and
how if it is to be useful.

@subsection[When Is There a Bug]

  If Emacs executes an illegal instruction, or dies with an operating
system error message that indicates a problem in the program (as opposed to
``disk full''), then it is certainly a bug.

  If Emacs updates the display in a way that does not correspond to what is
in the buffer, then it is certainly a bug.  If a command seems to do the
wrong thing but the problem corrects itself if you type @kbd{C-l}, it is a
case of incorrect display updating.

  Taking forever to complete a command can be a bug, but you must make
certain that it was really Emacs's fault.  Some commands simply take a long
time.  Type @kbd{C-g} and then @kbd{C-h l} to see whether the input Emacs
received was what you intended to type; if the input was such that you
@var[know] it should have been processed quickly, report a bug.  If you
don't know whether the command should take a long time, find out by looking
in the manual or by asking for assistance.

  If a command you are familiar with causes an Emacs error message in a
case where its usual definition ought to be reasonable, it is probably a
bug.

  If a command does the wrong thing, that is a bug.  But be sure you know
for certain what it ought to have done.  If you aren't familiar with the
command, or don't know for certain how the command is supposed to work,
then it might actually be working right.  Rather than jumping to
conclusions, show the problem to someone who knows for certain.

  Finally, a command's intended definition may not be best for editing
with.  This is a very important sort of problem, but it is also a matter of
judgment.  Also, it is easy to come to such a conclusion out of ignorance
of some of the existing features.  It is probably best not to complain
about such a problem until you have checked the documentation in the usual
ways, feel confident that you understand it, and know for certain that what
you want is not available.  If you are not sure what the command is
supposed to do after a careful reading of the manual, check the index and
glossary for any terms that may be unclear.  If you still do not
understand, this indicates a bug in the manual.  The manual's job is to
make everything clear.  It is just as important to report documentation
bugs as program bugs.

  If the on-line documentation string of a function or variable disagrees
with the manual, one of them must be wrong, so report the bug.

@subsection[How to Report a Bug]

@cfindex{emacs-version}
  When you decide that there is a bug, it is important to report it and to
report it in a way which is useful.  What is most useful is an exact
description of what commands you type, starting with the shell command to
run Emacs, until the problem happens.  Always include the version number
of Emacs that you are using; type @kbd{M-x emacs-version} to print this.

  The most important principle in reporting a bug is to report @var[facts],
not hypotheses or categorizations.  It is always easier to report the facts,
but people seem to prefer to strain to posit explanations and report
them instead.  If the explanations are based on guesses about how Emacs is
implemented, they will be useless; we will have to try to figure out what
the facts must have been to lead to such speculations.  Sometimes this is
impossible.  But in any case, it is unnecessary work for us.

  For example, suppose that you type @kbd{C-x C-f /glorp/baz.ugh
@key(RET)}, visiting a file which (you know) happens to be rather large,
and Emacs prints out @samp{I feel pretty today}.  The best way to report
the bug is with a sentence like the preceding one, because it gives all the
facts and nothing but the facts.

  Do not assume that the problem is due to the size of the file and say,
``When I visit a large file, Emacs prints out @samp{I feel pretty today}.''
This is what we mean by ``guessing explanations''.  The problem is just as
likely to be due to the fact that there is a @code{z} in the file name.  If
this is so, then when we got your report, we would try out the problem with
some ``large file'', probably with no @code{z} in its name, and not find
anything wrong.  There is no way in the world that we could guess that we
should try visiting a file with a @code{z} in its name.

  Alternatively, the problem might be due to the fact that the file starts
with exactly 25 spaces.  For this reason, you should make sure that you
inform us of the exact contents of any file that is needed to reproduce the
bug.  What if the problem only occurs when you have typed the @kbd{C-x C-a}
command previously?  This is why we ask you to give the exact sequence of
characters you typed since starting to use Emacs.

  You should not even say ``visit a file'' instead of @kbd{C-x C-f} unless
you @i[know] that it makes no difference which visiting command is used.
Similarly, rather than saying ``if I have three characters on the line,''
say ``after I type @kbd{@key(RET) A B C @key(RET) C-p},'' if that is
the way you entered the text.

  If you are not in Fundamental mode when the problem occurs, you should
say what mode you are in.

  Check whether any programs you have loaded into the Lisp world, including
your @code{.emacs} file, set any variables that may affect the functioning
of Emacs.  Also, see whether the problem happens in a freshly started Emacs
without loading your @code{.emacs} file (start Emacs with the @code{-q}
switch to prevent loading the init file.)  If the problem does @var[not]
occur then, it is essential that we know the contents of any programs that
you must load into the Lisp world in order to cause the problem to occur.

  If the problem does depend on an init file or other Lisp programs that
are not part of the Lisp Machine system, then you should make sure it is
not a bug in those programs by complaining to their maintainers, first.
After they verify that they are using Emacs in a way that is supposed to
work, they should report the bug.

  If you can tell us a way to cause the problem without visiting any files,
please do so.  This makes it much easier to debug.  If you do need files,
make sure you arrange for us to see their exact contents.  For example, it
can often matter whether there are spaces at the ends of lines, or a
newline after the last line in the buffer (nothing ought to care whether
the last line is terminated, but tell that to the bugs).

  The easy way to record the input to Emacs precisely is to to write a
dribble file; execute the Lisp expression
@example
(open-dribble-file "~/dribble")
@end example
@nopara
using @kbd{Meta-@key(ESC)} or from the @code{*scratch*} buffer just
after starting Emacs.  From then on, all Emacs input will be written in the
specified dribble file until the Emacs process is killed.

  For possible display bugs, it is important to report the terminal type
(the value of environment variable TERM), the termcap entry for the
terminal (since @code{/etc/termcap} is not identical on all machines), and
the output that Emacs actually sent to the terminal.  The way to collect
this output is to execute the Lisp expression
@example
(open-termscript "~/termscript")
@end example
@nopara
using @kbd{Meta-@key(ESC)} or from the @code{*scratch*} buffer just
after starting Emacs.  From then on, all output from Emacs to the terminal
will be written in the specified termscript file as well, until the Emacs
process is killed.  If the problem happens when Emacs starts up, put this
expression into your @code{~/.emacs} file so that the termscript file will
be open when Emacs displays the screen for the first time.

  The address for reporting bugs is
@format
GNU Emacs Bugs
545 Tech Sq, rm 703
Cambridge, MA 02139
@end format
@nopara
or, on Usenet, mail to @samp{eddie!bug-gnu-emacs%prep}.

@unnumbered[The GNU Manifesto]

@unnumberedsec[What's GNU?  Gnu's Not Unix!]

GNU, which stands for Gnu's Not Unix, is the name for the complete
Unix-compatible software system which I am writing so that I can give it
away free to everyone who can use it.  Several other volunteers are
helping me.  Contributions of time, money, programs and equipment are
greatly needed.

So far we have a portable C and Pascal compiler which compiles for Vax
and 68000 (though needing much rewriting), an Emacs-like text editor with
Lisp for writing editor commands, a yacc-compatible parser generator, a
linker, and around 35 utilities.  A shell (command interpreter) is nearly
completed.  When the kernel and a debugger are written, by the end of 1985
I hope, it will be possible to distribute a GNU system suitable for
program development.  After this we will add a text formatter, an Empire
game, a spreadsheet, and hundreds of other things, plus on-line
documentation.  We hope to supply, eventually, everything useful that
normally comes with a Unix system, and more.

GNU will be able to run Unix programs, but will not be identical
to Unix.  We will make all improvements that are convenient, based
on our experience with other operating systems.  In particular,
we plan to have longer filenames, file version numbers, a crashproof
file system, filename completion perhaps, terminal-independent
display support, and eventually a Lisp-based window system through
which several Lisp programs and ordinary Unix programs can share a screen.
Both C and Lisp will be available as system programming languages.
We will try to support UUCP, MIT Chaosnet, and Internet protocols
for communication.

GNU is aimed initially at machines in the 68000/16000 class, with
virtual memory, because they are the easiest machines to make it run
on.  The extra effort to make it run on smaller machines will be left
to someone who wants to use it on them.

@unnumberedsec[Why I Must Write GNU]

I consider that the golden rule requires that if I like a program I
must share it with other people who like it.  Software sellers want
to divide the users and conquer them, making each user agree not to
share with others.  I refuse to break solidarity with other users in
this way.  I cannot in good conscience sign a nondisclosure agreement
or a software license agreement.  For years I worked within the
Artificial Intelligence Lab to resist such tendencies and other
inhospitalities, but now they have gone too far: I cannot remain in
an institution where such things are done for me against my will.

So that I can continue to use computers without dishonor, I have
decided to put together a sufficient body of free software so that I
will be able to get along without any software that is not free.  I
have resigned from the AI lab to deny MIT any legal excuse to prevent
me from giving GNU away.

@unnumberedsec[Why GNU Will Be Compatible with Unix]

Unix is not my ideal system, but it is not too bad.  The essential
features of Unix seem to be good ones, and I think I can fill in what
Unix lacks without spoiling them.  And a system compatible with Unix
would be convenient for many other people to adopt.

@unnumberedsec[How GNU Will Be Available]

GNU is not in the public domain.  Everyone will be permitted to
modify and redistribute GNU, but no distributor will be allowed to
restrict its further redistribution.  That is to say, proprietary
modifications will not be allowed.  I want to make sure that all
versions of GNU remain free.

@unnumberedsec[Why Many Other Programmers Want to Help]

I have found many other programmers who are excited about GNU
and want to help.

Many programmers are unhappy about the commercialization of system
software.  It may enable them to make more money, but it requires them
to feel in conflict with other programmers in general rather than feel
as comrades.  The fundamental act of friendship among programmers is the
sharing of programs; marketing arrangements now typically used
essentially forbid programmers to treat others as friends.  The
purchaser of software must choose between friendship and obeying the
law.  Naturally, many decide that friendship is more important.  But
those who believe in law often do not feel at ease with either choice.
They become cynical and think that programming is just a way of making
money.

By working on and using GNU rather than proprietary programs, we can be
hospitable to everyone and obey the law.  In addition, GNU serves as an
example to inspire and a banner to rally others to join us in sharing.
This can give us a feeling of harmony which is impossible if we use
software that is not free.  For about half the programmers I talk to,
this is an important happiness that money cannot replace.

@unnumberedsec[How You Can Contribute]

I am asking computer manufacturers for donations of machines and money.
I'm asking individuals for donations of programs and work.

One consequence you can expect if you donate machines is that GNU
will run on them at an early date.  The machines should be complete,
ready to use systems, approved for use in a residential area, and not
in need of sophisticated cooling or power.

I have found very many programmers eager to contribute part-time work
for GNU.  For most projects, such part-time distributed work would be
very hard to coordinate; the independently-written parts would not work
together.  But for the particular task of replacing Unix, this problem
is absent.  A complete Unix system contains hundreds of utility programs,
each of which is documented separately.  Most interface specifications
are fixed by Unix compatibility.  If each contributor can write a
compatible replacement for a single Unix utility, and make it work
properly in place of the original on a Unix system, then these utilities
will work right when put together.  Even allowing for Murphy to create a few
unexpected problems, assembling these components will be a feasible task.
(The kernel will require closer communication and will be worked
on by a small, tight group.)

If I get donations of money, I may be able to hire a few people full or
part time.  The salary won't be high by programmers' standards, but I'm
looking for people for whom building community spirit is as important as
making money.  I view this as a way of enabling dedicated people to
devote their full energies to working on GNU by sparing them the need to
make a living in another way.

@unnumberedsec[Why All Computer Users Will Benefit]

Once GNU is written, everyone will be able to obtain good system
software free, just like air.

This means much more than just saving everyone the price of a Unix
license.  It means that much wasteful duplication of system programming
effort will be avoided.  This effort can go instead into advancing the
state of the art.

Complete system sources will be available to everyone.  As a result,
a user who needs changes in the system will always be free to make them
himself, or hire any available programmer or company to make them for him.
Users will no longer be at the mercy of one programmer or company which
owns the sources and is in sole position to make changes.

Schools will be able to provide a much more educational environment
by encouraging all students to study and improve the system code.
Harvard's computer lab used to have the policy that no program could
be installed on the system if its sources were not on public display,
and upheld it by actually refusing to install certain programs.
I was very much inspired by this.

Finally, the overhead of considering who owns the system software
and what one is or is not entitled to do with it will be lifted.

Arrangements to make people pay for using a program, including
licensing of copies, always incur a tremendous cost to society through
the cumbersome mechanisms necessary to figure out how much (that is,
which programs) a person must pay for.  And only a police state can
force everyone to obey them.  Consider a space station where air must
be manufactured at great cost: charging each breather per liter of air
may be fair, but wearing the metered gas mask all day and all night
is intolerable even if everyone can afford to pay the air bill.  And the
TV cameras everywhere to see if you ever take the mask off are outrageous.
It's better to support the air plant with a head tax and chuck the masks.

Copying all or parts of a program is as natural to a programmer as
breathing, and as productive.  It ought to be as free.

@unnumberedsec[Some Easily Rebutted Objections to GNU's Goals]

@quotation
``Nobody will use it if it is free, because that means
they can't rely on any support.''

``You have to charge for the program
to pay for providing the support.''
@end quotation

If people would rather pay for GNU plus service than get
GNU free without service, a company to provide just service
to people who have obtained GNU free ought to be profitable.

We must distinguish between support in the form of real programming work
and mere handholding.  The former is something one cannot rely on from a
software vendor.  If your problem is not shared by enough people, the
vendor will tell you to get lost.

If your business needs to be able to rely on support, the only way is to
have all the necessary sources and tools.  Then you can hire any
available person to fix your problem; you are not at the mercy of any
individual.  With Unix, the price of sources puts this out of
consideration for most businesses.  With GNU this will be easy.
It is still possible for there to be no available competent person, but
this problem cannot be blamed on distibution arrangements.  GNU does not
eliminate all the world's problems, only some of them.

Meanwhile, the users who know nothing about computers need handholding:
doing things for them which they could easily do themselves but don't
know how.

Such services could be provided by companies that sell just
hand-holding and repair service.  If it is true that users would
rather spend money and get a product with service, they will also be
willing to buy the service having got the product free.  The service
companies will compete in quality and price; users will not be tied to
any particular one.  Meanwhile, those of us who don't need the service
should be able to use the program without paying for the service.

@quotation
``You cannot reach many people without advertising,
and you must charge for the program to support that.''

``It's no use advertising a program people can get free.''
@end quotation

There are various forms of free or very cheap publicity that can be used
to inform numbers of computer users about something like GNU.  But it
may be true that one can reach more microcomputer users with
advertising.  If this is really so, a business which advertises the
service of copying and mailing GNU for a fee ought to be successful
enough to pay for its advertising and more.  This way, only the users
who benefit from the advertising pay for it.

On the other hand, if many people get GNU from their friends, and such
companies don't succeed, this will show that advertising was not
really necessary to spread GNU.  Why is it that free market advocates
don't want to let the free market decide this?

@quotation
``My company needs a proprietary operating system
to get a competitive edge.''
@end quotation

GNU will remove operating system software from the realm of
competition.  You will not be able to get an edge in this area, but
neither will your competitors be able to get an edge over you.  You
and they will compete in other areas, while benefitting mutually in
this one.  If your business is selling an operating system, you will
not like GNU, but that's tough on you.  If your business is something
else, GNU can save you from being pushed into the expensive business
of selling operating systems.

I would like to see GNU development supported by gifts from many
manufacturers and users, reducing the cost to each.

@quotation
``Don't programmers deserve a reward for their creativity?''
@end quotation

If anything deserves a reward, it is social contribution.  Creativity
can be a social contribution, but only in so far as society is free to
use the results.  If programmers deserve to be rewarded for creating
innovative programs, by the same token they deserve to be punished if
they restrict the use of these programs.

@quotation
``Shouldn't a programmer be able to ask for a reward for his creativity?''
@end quotation

There is nothing wrong with wanting pay for work, or seeking to maximize
one's income, as long as one does not use means that are destructive.
But the means customary in the field of software today are based on
destruction.

Extracting money from users of a program by restricting their use of
it is destructive because the restrictions reduce the amount and the
ways that the program can be used.  This reduces the amount of wealth
that humanity derives from the program.  When there is a deliberate
choice to restrict, the harmful consequences are deliberate destruction.

The reason a good citizen does not use such destructive means to
become wealthier is that, if everyone did so, we would all become
poorer from the mutual destructiveness.  This is Kantian ethics; or,
the Golden Rule.  Since I do not like the consequences that result if
everyone hoards information, I am required to consider it wrong for
one to do so.  Specifically, the desire to be rewarded for one's
creativity does not justify depriving the world in general of all or
part of that creativity.

@quotation
``Won't programmers starve?''
@end quotation

I could answer that nobody is forced to be a programmer.  Most of us
cannot manage to get any money for standing on the street and making
faces.  But we are not, as a result, condemned to spend our lives
standing on the street making faces, and starving.  We do something
else.

But that is the wrong answer because it accepts the questioner's
implicit assumption: that without ownership of software, programmers
cannot possibly be paid a cent.  Supposedly it is all or nothing.

The real reason programmers will not starve is that it will still be
possible for them to get paid for programming; just not paid as much
as now.

Restricting copying is not the only basis for business in software.
It is the most common basis because it brings in the most money.  If
it were prohibited, or rejected by the customer, software business
would move to other bases of organization which are now used less often.
There are always numerous ways to organize any kind of business.

Probably programming will not be as lucrative on the new basis as it
is now.  But that is not an argument against the change.  It is not
considered an injustice that sales clerks make the salaries that they
now do.  If programmers made the same, that would not be an injustice
either.  (In practice they would still make considerably more than
that.)

@quotation
``Don't people have a right to control how their creativity is used?''
@end quotation

``Control over the use of one's ideas'' really constitutes control over
other people's lives; and it is usually used to make their lives more
difficult.

People who have studied the issue of intellectual property rights
carefully (such as lawyers) say that there is no intrinsic right to
intellectual property.  The kinds of supposed intellectual property
rights that the government recognizes were created by specific acts of
legislation for specific purposes.

For example, the patent system was established to encourage inventors to
disclose the details of their inventions.  Its purpose was to help
society rather than to help inventors.  At the time, the life span of 17
years for a patent was short compared with the rate of advance of the
state of the art.  Since patents are an issue only among manufacturers,
for whom the cost and effort of a license agreement are small compared
with setting up production, the patents often do not do much harm.  They
do not obstruct most individuals who use patented products.

The idea of copyright did not exist in ancient times, when authors
frequently copied other authors at length in works of non-fiction.  This
practice was useful, and is the only way many authors's works have
survived even in part.  The copyright system was created expressly for
the purpose of encouraging authorship.  In the domain for which
it was invented---books, which could be copied economically only on
a printing press---it did little harm, and did not obstruct
most of the individuals who read the books.

All intellectual property rights are just licenses granted by society
because it was thought, rightly or wrongly, that society as a whole
would benefit by granting them.  But in any particular situation, we
have to ask: are we really better off granting such license?  What kind
of act are we licensing a person to do?

The case of programs today is very different from that of books
a hundred years ago.  The fact that the easiest way to copy a program
is from one neighbor to another, the fact that a program has both
source code and object code which are distinct, and the fact that
a program is used rather than read and enjoyed, combine to create
a situation in which a person who enforces a copyright is harming
society as a whole both materially and spiritually; in which a
person should not do so regardless of whether the law enables him to.

@quotation
``Competition makes things get done better.''
@end quotation

The paradigm of competition is a race: by rewarding the winner, we
encourage everyone to run faster.  When capitalism really works this
way, it does a good job; but its defenders are wrong in assuming it
always works this way.  If the runners forget why the reward is
offered and become intent on winning, no matter how, they may find
other strategies---such as, attacking other runners.  If the
runners get into a fist fight, they will all finish late.

Proprietary and secret software is the moral equivalent of runners in
a fist fight.  Sad to say, the only referree we've got does not seem
to object to fights; he just regulates them (``For every ten yards you
run, you can fire one shot'').  He really ought to break them up,
and penalize runners for even trying to fight.

@quotation
``Won't everyone stop programming without a monetary incentive?''
@end quotation

Actually, many people will program with absolutely no monetary
incentive.  Programming has an irresistible fascination for some
people, usually the people who are best at it.  There is no shortage
of professional musicians who keep at it even though they have no
hope of making a living that way.

But really this question, though commonly asked, is not appropriate to
the situation.  Pay for programmers will not disappear, only become
less.  So the right question is, will anyone program with a reduced
monetary incentive?  My experience shows that they will.

For more than ten years, many of the world's best programmers worked at
the Artificial Intelligence Lab for far less money than they could have
had anywhere else.  They got many kinds of non-monetary rewards: fame
and appreciation, for example.  And creativity is also fun, a reward in
itself.

Then most of them left when offered a chance to do the same interesting
work for a lot of money.

What the facts show is that people will program for reasons other than
riches; but if given a chance to make a lot of money as well, they will
come to expect and demand it.  Low-paying organizations do poorly
in competition with high-paying ones, but they do not have to do
badly if the high-paying ones are banned.

@quotation
``We need the programmers desperately.  If they demand that we
stop helping our neighbors, we have to obey.''
@end quotation

You're never so desperate that you have to obey this sort of demand.
Remember: millions for defense, but not a cent for tribute!

@quotation
``Programmers need to make a living somehow.''
@end quotation

In the short run, this is true.  However, there are plenty of ways
that programmers could make a living without selling the right to use
a program.  This way is customary now because it brings programmers
and businessmen the most money, not because it is the only way to make
a living.  It is easy to find other ways if you want to find them.
Here are a number of examples.

A manufacturer introducing a new computer will pay for
the porting of operating systems onto the new hardware.

The sale of teaching, hand-holding and maintenance services could
also employ programmers.

People with new ideas could distribute programs as freeware, asking
for donations from satisfied users, or selling hand-holding services.
I have met people who are already working this way successfully.

Users with related needs can form users' groups, and pay dues.  A
group would contract with programming companies to write programs that
the group's members would like to use.

All sorts of development can be funded with a Software Tax:

@quotation
Suppose everyone who buys a computer has to pay x percent of
the price as a software tax.  The government gives this to
an agency like the NSF to spend on software development.

But if the computer buyer makes a donation to software development
himself, he can take a credit against the tax.  He can donate to
the project of his own choosing---often, chosen because he hopes to
use the results when it is done.  He can take a credit for any amount
of donation up to the total tax he had to pay.

The total tax rate could be decided by a vote of the payers of
the tax, weighted according to the amount they will be taxed on.

The consequences:
@itemize @bullet
@item
The computer-using community supports software development.
@item
This community decides what level of support is needed.
@item
Users who care which projects their share is spent on
can choose this for themselves.
@end itemize
@end quotation

In the long run, making programs free is a step toward the post-scarcity
world, where nobody will have to work very hard just to make a living.
People will be free to devote themselves to activities that are fun,
such as programming, after spending the necessary ten hours a week
on required tasks such as legislation, family counseling, robot
repair and asteroid prospecting.  There will be no need to be able
to make a living from programming.

We have already greatly reduced the amount of work that the whole
society must do for its actual productivity, but only a little of this
has translated itself into leisure for workers because much
nonproductive activity is required to accompany productive activity.
The main causes of this are bureaucracy and isometric struggles
against competition.  Free software will greatly reduce these
drains in the area of software production.  We must do this,
in order for technical gains in productivity to translate into
less work for us.

@include ggloss.tex

@unnumbered Key (Character) Index
@printindex kw

@unnumbered Command Index
@printindex cf

@unnumbered Variable Index
@printindex vr

@unnumbered Concept Index
@printindex cp

@summarycontents
@contents
@bye
